等效于Angular 7的$ location.search() [英] $location.search() equivalent in Angular 7
问题描述
我正在将Angular JS迁移到Angular7.我正在查看这段代码,并尝试在Angular 7中实现.
I am migrating Angular JS to Angular 7. I am looking at this code and trying to implement in Angular 7.
在服务中,注入了$location
,以下方法重置并设置了查询参数.
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
});
}
如何在Angular 7中实现呢?
How do I implement this in Angular 7?
推荐答案
在Angular v7中,参数是完全不同的,因为它们是路由"的一部分.因此,没有直线等效于您要完成的工作.
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.
在Angular v2 +中,存在三种不同类型的参数,因此第一步是定义所需的类型.
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:
假设您要坚持使用查询参数:
Assuming you want to stick with Query parameters:
您可以像这样在HTML中进行设置:
You can set them in the HTML like this:
<a [routerLink]="[product.id]"
[queryParams]="{filterBy: listFilter, showImage: showImage}">
{{ product.productName }}
</a>
或者在这样的代码中:
this.router.navigate([`/search`],
{queryParams: {
searchType: searchType,
searchField: searchField, // ...
}});
这篇关于等效于Angular 7的$ location.search()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!