Angular 2获取父级激活路线 [英] Angular 2 get parent activated route
问题描述
我有一条路线,上面有路线孩子:
I have a route with route children like this:
{
path: 'dashboard',
children: [{
path: '',
canActivate: [CanActivateAuthGuard],
component: DashboardComponent
}, {
path: 'wage-types',
component: WageTypesComponent
}]
}
在浏览器中,我想获得激活的父路由,如
And in the browser I want to get the activated parent route like
host.com/dashboard/wage-types
如何获取/dashboard
,但是可以在Angular 2中获得,而不是在JavaScript中获得,但是我也可以接受JavaScript代码,但可以接受主要Angular 2.
How to get the /dashboard
but possible with Angular 2 and not in JavaScript, but also I can accept JavaScript code too but primary Angular 2.
推荐答案
您可以通过使用ActivatedRoute上的parent属性来完成此操作-像这样.
You can do this by using the parent property on the ActivatedRoute - something like this.
export class MyComponent implement OnInit {
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.activatedRoute.parent.url.subscribe((urlPath) => {
const url = urlPath[urlPath.length - 1].path;
})
}
}
您可以在此处更详细地查看ActivatedRoute中的所有内容: https://angular.io/api/router/ActivatedRoute
You can see everything from the ActivatedRoute in more detail here: https://angular.io/api/router/ActivatedRoute
这篇关于Angular 2获取父级激活路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!