gpt4 book ai didi

Angular 2获取父激活路由

转载 作者:太空狗 更新时间:2023-10-29 17:42:56 24 4
gpt4 key购买 nike

我有一条路线,路线 child 是这样的:

{
path: 'dashboard',
children: [{
path: '',
canActivate: [CanActivateAuthGuard],
component: DashboardComponent
}, {
path: 'wage-types',
component: WageTypesComponent
}]
}

在浏览器中,我想获取激活的父路由,如

host.com/dashboard/wage-types

如何获取 /dashboard 但可以使用 Angular 2 而不是 JavaScript,但我也可以接受 JavaScript 代码但主要是 Angular 2。

最佳答案

您可以通过使用 ActivatedRoute 上的父属性来做到这一点 - 就像这样。

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

关于Angular 2获取父激活路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com