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屋!