Angular2 get current route's alias

后端 未结 5 1915
情深已故
情深已故 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:15

    For >= Angular2 RC.x (new router)

    There are no aliases anymore in the new router.

    You can get the relative route of the local component

      routerOnActivate(curr:RouteSegment, prev?:RouteSegment, currTree?:RouteTree, prevTree?:RouteTree):void {
        console.log(curr.stringifiedUrlSegments);
      }
    

    or the full path using

    constructor(private router:Router) {}
    
      routerOnActivate(curr:RouteSegment, prev?:RouteSegment, currTree?:RouteTree, prevTree?:RouteTree):void {
        console.log(this.router.serializeUrl(tree));
      }
    

    or

    constructor(router:Router, private location:Location) {
      router.changes.subscribe(() => {
        console.log(this.location.path());
      });
    }
    

提交回复
热议问题