【发布时间】:2017-08-31 03:37:56
【问题描述】:
我有以下路线,我正在尝试获取子组件的路径。
const myRoutes: Routes = [
{
path: '',
component: mainComponent
}, {
path: ':id/create',
component: anothercomponent,
children: [
{ path: '', redirectTo : 'general-information', pathMatch: 'full' },
{ path: 'general-information', component: GeneralInfoComponent},
{ path: 'another-info', component: AnotherInfoComponent}
]
}]
为了在重新加载时获取当前路径段,我正在做这样的事情
route.firstChild.url.subscribe(val => {
let myRoute = val[0].path;
});
即使我订阅了 url(在另一个组件中),如果子路由在 :id/create 下更改,即如果它从 022/create/general-information 更改为 022/create/another-info,我也不会收到触发器为了让子路线的每个路段都发生变化,我必须做类似
的事情router.events.subscribe(event:Event => {
if(event instanceof NavigationEnd) {
this.route.firstChild.url.subscribe(val => {
let myroute = val[0].path;
});
}
})
有没有更简单的方法来获取路径?就像一般信息或其他信息?如果是,请提供解决方案,如果不是,是什么原因。 希望我解释得很好,但如果需要更多说明,请告诉我。提前致谢。
【问题讨论】:
标签: angular typescript angular2-routing