Angular 6 router - 替换当前路由的部分参数 [英] Angular 6 router - replace some of the parameters of the current route

查看:43
本文介绍了Angular 6 router - 替换当前路由的部分参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Angular 6 解决方案中,网址具有以下结构:

In my Angular 6 solution, the urls have this structure:

/{language}/{app_section}/{object_id}/{view}?queryparams...

/{language}/{app_section}/{object_id}/{view}?queryparams...

语言选择器组件由应用程序的所有部分共享,并包含在其中一个父路由的模板中,以便它出现在所有子路由中.

A language selector component is shared by all sections of the application, and is included in the template of one of the parent routes so that it appears in all the children routes.

当用户选择一种新语言时,组件应该只替换当前路由的 {language} 部分,其他所有内容保持不变,并导航到新路由.看起来应该很简单,但我似乎找不到简单的方法.有人可以帮忙吗?

When a user selects a new language, the component should replace only the {language} segment of the current route, leaving everything else unchanged, and navigate to the new route. It seems it should be straightforward to do, but I can't seem to find an easy way. Can anybody help?

推荐答案

是的,这是可能的.阅读当前路线,替换语言并导航到该网址.

Yep it is possible. Read the current route, replace the language and navigate to that URL.

constructor(private _router: Router ) {
}

languageSelected(newLanguage) {
  const newUrl = this._router.url.replace(this.currentlanguage, newLanguage);
  this.router.navigateByUrl(newUrl);
}

我假设 this.currentLanguage 包含与 URL 中表示的完全相同的当前语言,并且传递给该方法的 newLanguage 也是网址.诸如此类的技术将触发组件刷新并且会丢失临时状态.

I've assumed that this.currentLanguage contains the current language exactly as represented in the URL and the newLanguage passed to the method is also in the exact form required by the URL. A technique such as this will trigger a component refresh and temporary state will be lost.

这篇关于Angular 6 router - 替换当前路由的部分参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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