本文介绍了使用新的Router v3 Angular2清除所有queryParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何将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导航到另一个页面,它不会覆盖前一个页面,而是在URL中添加另一个?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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:47