问题
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