如何获取当前路由器出口组件的静态路由路径,路由器事件中的角度激活了什么?
我在app.component 中需要它,因为如果我必须在所有组件中使用activatedRoute.snapshot 进行操作,那将是多余的
self.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
//I need here the path of route E.G '/page/:id/' from root
}
});
Run Code Online (Sandbox Code Playgroud)
更新:在路由中:
export const MAIN_ROUTES: Routes = [
{ path: '', component: HomePage },
{ path: 'something', component: SomethingComponent },
{ path: 'page/:id', component: PageComponent }, //if the url is 'example.com/page/someId' than, I want to get the "path" field of this object and if I change the page, I need that inside the router event above.
]
Run Code Online (Sandbox Code Playgroud)
我想我为他找到了一个古怪的解决方案:
let path = '';
self.router.events.subscribe((event) => {
console.log(event);
if(event instanceof ActivationEnd) {
if(event.snapshot) {
if(event.snapshot.routeConfig && event.snapshot.routeConfig.path) {
path = path == '' ? event.snapshot.routeConfig.path : [event.snapshot.routeConfig.path, path].join('/');
}
}
}
if (event instanceof NavigationEnd) {
//save the path, and clear it.
self.path = path;
path = '';
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6634 次 |
| 最近记录: |