这些是我的路线
const routes: Routes = [
{
path: 'path1',
loadChildren: () => import('./child/child.module').then(r => r.ChildModule),
},
{
path: 'path2',
loadChildren: () => import('./child/child.module').then(r => r.ChildModule),
}]
我想得到";路径1";或";路径2";在ChildModule 的构造函数中
export class ChildModule {
constructor(router: Router) {
// I'd like to get them here
}
}
使用ActivatedRoute代替路由器访问激活的路由属性。
import {ActivatedRoute} from "@angular/router";
export class ChildModule {
constructor(route: ActivatedRoute) {
this.route.queryParams;
this.route.params;
this.route.data;
}
}
this.router.url
将返回完整路径/.../path1
。如果该路由没有父路由,则只返回/path1
。
要返回路径的末尾,可以使用
let path = this.router.url.split('/').pop(0);