Retrieving the active component and path

前端 未结 5 705
既然无缘
既然无缘 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:30

    Yes you can check current active component using router snapshot-

    import { Router,ActivatedRoute }      from '@angular/router';
    
    constructor(public route : ActivatedRoute) {
        var snapshot = route.snapshot;
        console.log(snapshot._routeConfig.component.name); //This will give you the name of current active component
      }
    

    Note- snapshot._routeConfig.component.name it will give you the active component name, and if you want url you can also get it by .url instead of name

提交回复
热议问题