2017-01-05 8 views
4

サブスクライブされたrouter.events内のparamsのようなアクティブなルートデータを取得するにはどうすればよいですか?router.events内の有効なルートデータを取得

私はではありません this.route.paramsを購読します!

// Catch any events in a certain component, where I subscribe to it! 
this.router.events.subscribe(event => 
{ 
// The event object has only the url (when is NavigationStart/End), there is nothing useful for me 


}); 

UPDATE

私はルータのイベントに反応する方法を尋ねていないので、私の質問は違います!

私はrouter.events内でactivate routes paramsを取得する方法を尋ねます!

それには違いがあります!

+0

[角2ルータイベントリスナー]の複製が可能です(http://stackoverflow.com/questions/35912932/angular-2-router-event-listener) –

+0

重複はありません私の説明を読んでください! – Pascal

答えて

0
this.router.events.subscribe(event => 
{ 
    console.log(this.router.routerState.root); 
    console.log(this.router.routerState.root.firstChild); 
    console.log(this.router.routerState.root.firstChild && this.router.routerState.root.firstChild.firstChild); 
}); 
+1

それは公正ではありませんあなたは2つの解決策を望んでいます:P btw。私はあなたが他のスレッドに投稿したものを見て前に尋ねました... – Pascal

+0

人生は公正ではありません; p –

+1

私はあなたから期待した正確な反応:P – Pascal

3

このお試しください:

import { ActivatedRoute } from '@angular/router'; 

@Component({ 
    ... 
}) 
export class TestComponent implements OnInit{ 
    constructor(protected route: ActivatedRoute) {} 
    ngOnInit() { 
     let name = this.route.snapshot.params['name']; 
    } 
} 

これは、現在アクティブなルートを注入し、後者あなたはそのルートから情報を取得できます。

関連する問題