$location.search() equivalent in Angular 7

删除回忆录丶 提交于 2019-12-24 12:20:10

问题


I am migrating Angular JS to Angular 7. I am looking at this code and trying to implement in Angular 7.

In the service, $location was injected, and the following methods reset and set the query parameters.

function resetAllOptions() {
            // Clears query params
            $location.search('');

}

function setQueryParameters() {
    // Sets query parameters
            $location.search({
                searchType: searchType,
                searchField: searchField,
                searchValue: searchValue,
                searchValueTwo: searchValueTwo,
                searchValueThree: searchValueThree
            });
}

How do I implement this in Angular 7?


回答1:


Parameters are done completely differently in Angular v7 as they are a part of Routing. So there is no direct line to line equivalent of what you are trying to accomplish.

In Angular v2+, there are three different types of parameters, so your first step is to define the type that you want.

Here is a post that describes the different types in detail:

Send data through routing paths in Angular

Assuming you want to stick with Query parameters:

You can set them in the HTML like this:

          <a [routerLink]="[product.id]"
             [queryParams]="{filterBy: listFilter, showImage: showImage}">
            {{ product.productName }}
          </a>

Or in code like this:

this.router.navigate([`/search`],
              {queryParams: {
                     searchType: searchType,
                     searchField: searchField, // ...
               }});


来源:https://stackoverflow.com/questions/54772646/location-search-equivalent-in-angular-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!