mor*_*tzg 4 angular-routing angular
我的 Angular 项目中有以下路由配置:
export const routes: Routes = [
{ path: 'a/:id', loadChildren: './a/a.module#AModule', data: {a: a}},
{ path: 'b/:id', loadChildren: './b/b.module#BModule', data: {a: b}}
];
Run Code Online (Sandbox Code Playgroud)
现在我可以像这样获取路由配置:
this.router.config //outputs the above array
Run Code Online (Sandbox Code Playgroud)
显示,但由于配置具有未解析的路径并且路由器保存已解析的路径,因此this.router.url我a如何正确识别正确的路径。Route
我想要这样做的原因是访问my所在data的位置,而不是组件本身(角度文档说该对象只能由路由组件本身访问。app.componentrouter-outletdata
您可以使用Router.state来获取原始Route配置。
由于 ,Angular 路由器将当前路由表示为分层路由树children,因此您需要遍历它以获取最深的子节点,这通常就是您正在寻找的。
可以通过以下方式完成:
let currentRoute = this.router.routerState.root;
while (currentRoute.firstChild) {
currentRoute = currentRoute.firstChild;
}
console.log(currentRoute.routeConfig); // returns `Route` object
Run Code Online (Sandbox Code Playgroud)
为了对变化做出反应,这应该在里面this.router.events.subscribe(...)。
| 归档时间: |
|
| 查看次数: |
7855 次 |
| 最近记录: |