2017-02-13 80 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