小编Ale*_*nin的帖子

Angular2-router:如何只改变路由的参数?

我希望在我的应用程序中有以下路由:

export const routes: Routes = [
    { 
       path: ':universityId', 
       component: UniversityComponent,
       children: [
           { path: 'info', component: UniversityInfoComponent },
           { path: 'courses/:status', component: CoursesByStatusComponent }
       ]
    }
]
Run Code Online (Sandbox Code Playgroud)

作为上/1/info/1/courses/open网址,我该如何改变:universityIdUniversityComponent

简单router.navigate(['../', 2], {relativeTo: currentRoute })不会这样做,因为它重定向到/2,丢失所有其他信息.使用'../2/courses/open也不是一种选择 - 那时我可以在任何儿童路线上.我能想到的最好的是:

const urlTree = this.router.parseUrl(this.router.url);
urlTree.root.children['primary'].segments[0].path = '2';
this.router.navigateByUrl(urlTree);
Run Code Online (Sandbox Code Playgroud)

但它有点难看

angular2-routing angular

9
推荐指数
1
解决办法
1097
查看次数

标签 统计

angular ×1

angular2-routing ×1