2017-02-13 79 views
1

如何获取不参与路由的组件中的ActivatedRoute?Angular 2,如何获得全局激活的路由?

我想是这样的:

constructor(
    private _route: ActivatedRoute, 
) { 
    this._route.params.subscribe((params: Params) => { 

    console.log(params['param1'], params['param2']); 

    }); 

} 

它记录不确定的,不确定的。

回答

1
constructor(private router:Router) { 
    router.events 
    .filter(e => e instanceof NavigationEnd) 
    .forEach(e => { 
    console.log(router.routerState.root); 
    console.log(router.routerState.root.firstChild); 
); 
} 
+0

太棒了。非常感谢!嘿,输出一组相当复杂的对象。我将如何去获取参数?和嵌套参数? – BBaysinger

+1

'... root.params ['id']'或'... firstChild.params ['id']'应该可以工作。使用上面的代码,你应该得到一个https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html如果我没有混合起来。 –

+0

有没有办法从服务中获取路由器?我的主要服务需要此信息。否则,我唯一的选择是捕获组件中的路由器状态,并将这些参数传递到我的服务中,这看起来有点不方便。 – BBaysinger