Retrieving the active component and path

前端 未结 5 537
既然无缘
既然无缘 2021-01-12 22:51

With Angular2, how would I retrieve the current active component and path?

For example I might have the following routes:

{ path: \'\', component:          


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 23:28

    Yah, the angular 2 starter guide covers all that, basically you need to inject ActivatedRoute on your component then you can retrieve the information you want by subscribing to the route params.

    I would really recommend you to do the angular tutorial tour of heroes, it is actually pretty good learning material that they provided.

    Inject ActivatedRoute, on your component constructor:

    constructor(private route: ActivatedRoute) {
    }
    
    ngOnInit() {
        this.route.params.subscribe(params => {
          console.log(params['id']);
        });
      }
    

提交回复
热议问题