Angular2,如何清除路由器导航上的 URL 查询参数 [英] Angular2, how to clear URL query params on router navigate

查看:54
本文介绍了Angular2,如何清除路由器导航上的 URL 查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Angular 2.4.2 的导航期间努力清除 URL 参数.我已尝试附加每个选项以清除以下导航行中的参数,或其中与导航或导航的任何混合,或navigateByUrl,但不知道如何完成.

I am struggling with clearing out URL params during nav in Angular 2.4.2. I have tried attaching every option to clear the params in the below navigation line, or any mix therein with navigate, or navigateByUrl, but cannot figure out how to accomplish.

this.router.navigateByUrl(this.router.url, { queryParams: {}, preserveQueryParams: false });

举个例子,我在路由 /section1/page1?key1=abc&key2=def 并希望保持在同一路由但删除所有 url 参数,所以最终结果应该是/section/page1

As an example I am at route /section1/page1?key1=abc&key2=def and want to stay at the same route but remove all the url parameters, so the end result should be /section/page1

推荐答案

正如 DeborahK 所指出的,当我导航到 this.router.url 时,该 URL 已经嵌入了 url 参数.为了解决这个问题,我将 URL 中的参数作为字符串剥离,然后使用 navigateByUrl 跳转到那里.

As DeborahK pointed out, when I was navigating to this.router.url, that URL already had the url params embedded. To solve I stripped the params off the URL as a string, and then used navigateByUrl to jump there.

let url: string = this.router.url.substring(0, this.router.url.indexOf("?"));
this.router.navigateByUrl(url);

这篇关于Angular2,如何清除路由器导航上的 URL 查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆