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

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

问题描述

在Angular 2.4.2中进行导航时,我正在努力清除URL参数.我尝试附加每个选项以清除下面导航行中的参数,或与Navigation或navigationByUrl混合使用的参数,但无法弄清楚如何完成.

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的参数,然后使用navigationByUrl跳到那里.

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天全站免登陆