Angular 2,如何全局激活路由?(Angular 2, how to get activated route globally?)
如何在不涉及路由的组件中获取ActivatedRoute?
我试过这样的:
constructor( private _route: ActivatedRoute, ) { this._route.params.subscribe((params: Params) => { console.log(params['param1'], params['param2']); }); }它记录未定义,未定义。
How can I get the ActivatedRoute in a component that is not involved with routing?
I tried like this:
constructor( private _route: ActivatedRoute, ) { this._route.params.subscribe((params: Params) => { console.log(params['param1'], params['param2']); }); }It logs undefined, undefined.
最满意答案
constructor(private router:Router) { router.events .filter(e => e instanceof NavigationEnd) .forEach(e => { console.log(router.routerState.root); console.log(router.routerState.root.firstChild); ); }在Angular 6中:
this.router.events.pipe(filter(e => e instanceof NavigationEnd)).subscribe(r => { console.log(r); }); constructor(private router:Router) { router.events .filter(e => e instanceof NavigationEnd) .forEach(e => { console.log(router.routerState.root); console.log(router.routerState.root.firstChild); ); }In Angular 6:
this.router.events.pipe(filter(e => e instanceof NavigationEnd)).subscribe(r => { console.log(r); });更多推荐
发布评论