Clear all queryParams with new Router v3 Angular2

扶醉桌前 提交于 2019-12-01 15:01:51

I struggled with this as well. You would expect the router to clear query params by default when navigating to another route...

You can do either

this._router.navigate(['/route1'], {queryParams: {}});

or

this._router.navigateByUrl('/route1');

or when using routerLink:

<a [routerLink]="['/applications']" [queryParams]="{}"> Applications</a>

Actually ... you should NOT "expect the router to clear query params".

Clearly you DO expect that. But that's because you don't know about an important router design decision ... probably because, unfortunately, we haven't told you about it in the docs yet. We're working on correcting that now.

QueryParams are for parameters that are global across navigations; they do not change.

MatrixParams are for parameters that belong to the current navigation; these do change.

What belongs in global QueryParams? Stuff that comes from the server are candidates. Like auth tokens that belong on every interaction.

But required route params (the :id in customer/:id) and optional matrix params (like the ;name=Jo* in /customers;name=Jo*) are local to one navigation. That's how you might specify a navigation for customers with an optional name filter.

Whether or not you agree with this approach, it is fundamental to the v.3 router design.

I suggest adjusting your expectations rather than fighting it.

You may wanna try this :

this.router.navigate(['target'], {preserveQueryParams: false});

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