Angular 2,如何全局激活路由?(Angular 2, how to get activated route globally?)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
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); });

更多推荐

本文发布于:2023-04-24 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a202973c131024f0b5f10693e83f0467.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   全局   Angular   globally   route

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!