Angular 6路由器-替换当前路由的某些参数 [英] Angular 6 router - replace some of the parameters of the current route
问题描述
在我的Angular 6解决方案中,URL具有以下结构:
In my Angular 6 solution, the urls have this structure:
/{语言}/{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?
推荐答案
是的.阅读当前路线,替换语言,然后导航到该URL.
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
也是URL所需的确切形式.诸如此类的技术将触发组件刷新,并且临时状态将丢失.
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路由器-替换当前路由的某些参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!