Angular2 get current route's alias

后端 未结 5 1839
情深已故
情深已故 2021-01-11 10:39

I\'m able to get current route\'s path like: /about, /, /news with location.path(). But how can I get its alias instead (the \'as\' part in the definition of a route)?

5条回答
  •  终归单人心
    2021-01-11 11:16

    You should not use location, instead you should use Router instance.

    In your component, inject it in constructor with:

    constructor(public router: Router)
    

    than, you could obtain name of component with:

    this.router.currentInstruction.component.routeName
    

    I am not sure how you obtain as from router config. But RouterLink directive should be right spot to start with: https://angular.io/docs/ts/latest/api/router/RouterLink-directive.html

提交回复
热议问题