Angular2-router:如何仅更改路线的参数? [英] Angular2-router: How to only change a parameter of a route?
问题描述
我想在我的应用程序中使用以下路由:
I want to have the following routes in my application:
export const routes: Routes = [
{
path: ':universityId',
component: UniversityComponent,
children: [
{ path: 'info', component: UniversityInfoComponent },
{ path: 'courses/:status', component: CoursesByStatusComponent }
]
}
]
在/1/info
或/1/courses/open
网址上,如何仅从UniversityComponent
更改:universityId
?
Being on /1/info
or /1/courses/open
url, how do I change :universityId
only from UniversityComponent
?
简单的router.navigate(['../', 2], {relativeTo: currentRoute })
不会执行操作,因为它会重定向到/2
,从而丢失所有其他信息.也不能使用'../2/courses/open
-那时我可以在任何子路径上.
我能想到的最好的是:
Simple router.navigate(['../', 2], {relativeTo: currentRoute })
won't do because it redirects to /2
, losing all other information. Using '../2/courses/open
is also not an option - I can be on any child route at that moment.
Best I could come up with is:
const urlTree = this.router.parseUrl(this.router.url);
urlTree.root.children['primary'].segments[0].path = '2';
this.router.navigateByUrl(urlTree);
但这有点丑陋
推荐答案
这很简单. 您可以简单地尝试使用 this.router.navigate(["../2","courses","open"],{relativeTo:this.route});
This is very simple. You can simply try to use this.router.navigate(["../2", "courses", "open"], {relativeTo: this.route});
(或)
this.router.navigate(["../2",课程/开放"],{relativeTo:this.route});
this.router.navigate(["../2", "courses/open"], {relativeTo: this.route});
这会将您重定向到../2/courses/open.
This will redirect you to ../2/courses/open.
这篇关于Angular2-router:如何仅更改路线的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!