问题描述
我正在尝试弄清楚如何使用带有查询参数的 Angular2 路由器导航(路由器 3.0.0-alpha.7).
I'm trying to figure out how to use the Angular2 router navigation (router 3.0.0-alpha.7) with query parameters.
我可以使用以下行轻松导航到带有 queryParam 的路线:
I can easily navigate to a route with a queryParam with this line:
this._router.navigate(['/login'], {queryParams: {redirect: 'route1'}});
在 '/login' 组件中,我做了一些登录,这将重定向到 redirect
路由,即这里的 route1.但是,在重定向之后,redirect
查询参数保留在 URL 中,即我现在位于页面 /route1?redirect=route1
.我想在这里删除重定向参数.
In the '/login' component, I do some login which will redirect to the redirect
route, namely route1 here. However, after the redirection, the redirect
query parameters stays in the URL, i.e. I'm now at page /route1?redirect=route1
. I want to remove the redirect parameter here.
此外,如果我随后导航到具有相同 redirect
queryParam 的另一个页面,它不会覆盖前一个页面,而是添加另一个 ?redirect=...
在网址中.即:
Moreover, if I then navigate to another page with the same redirect
queryParam, it doesn't overwrite the previous one, but adds another ?redirect=...
in the url. Namely:
this._router.navigate(['/another-route'], {queryParams: {redirect:'route2'}});
引导我到 /another-route?redirect=route2?redirect=route1
在路由之间导航时是否可以清除 queryParams?我试过 this._router.navigate(['/route1'], {queryParams: {redirect: null}});
或 {queryParams: null}
等但没有成功.
Is it possible to clear the queryParams when navigating between routes?I tried this._router.navigate(['/route1'], {queryParams: {redirect: null}});
, or {queryParams: null}
etc but no success.
推荐答案
我也遇到了这个问题.您希望路由器在导航到另一条路由时默认清除查询参数...
I struggled with this as well. You would expect the router to clear query params by default when navigating to another route...
你可以这样做
this._router.navigate(['/route1'], {queryParams: {}});
或
this._router.navigateByUrl('/route1');
或使用 routerLink
时:
<a [routerLink]="['/applications']" [queryParams]="{}"> Applications</a>
这篇关于使用新的 Router v3 Angular2 清除所有 queryParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!