Angular 获取当前活动组件路径
Angular get current active component path
我有以下路线,我正在尝试获取子组件的路径。
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(在 anotherComponent 中),如果子路由在 :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;
});
}
})
有没有更简单的方法只获取路径?就像一般信息或其他信息?如果是,请提供解决方案,如果不是,原因是什么。
希望我解释得很好,但如果需要更多说明,请告诉我。提前致谢。
一年后因为没有人回答你可以得到它:
this._activatedRoute.snapshot.routeConfig.path
如果你在路上,假设 /dashboard/graphs
这会给你 graphs
我有以下路线,我正在尝试获取子组件的路径。
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(在 anotherComponent 中),如果子路由在 :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;
});
}
})
有没有更简单的方法只获取路径?就像一般信息或其他信息?如果是,请提供解决方案,如果不是,原因是什么。 希望我解释得很好,但如果需要更多说明,请告诉我。提前致谢。
一年后因为没有人回答你可以得到它:
this._activatedRoute.snapshot.routeConfig.path
如果你在路上,假设 /dashboard/graphs
这会给你 graphs