2

I have two routes with a different path and name but with the same component. Is there a way to check what the path is when I navigate for ex. to /add-userOR the name for ex. AddUser

Routeconfig

app.component.ts

@RouteConfig([
      {
        path: '/add-user',
        name: 'AddUser',
        component: UserComponent
      },
      {
        path: '/user/:id/detail',
        name: 'UserDetail',
        component: UserComponent
      }
    ])

Update

Usercomponent

user.component.ts

ngOnInit() {
//this is pseudocode
   if (this._router.name == 'AddUser') {
        console.log('you want to add a user')
   }
   else{
        console.log('you want to see the details of a user');
   }
Claudiu Matei
  • 4,091
  • 3
  • 19
  • 33
  • 2
    Maybe also http://stackoverflow.com/questions/34323480/in-angular-2-how-do-you-determine-the-active-route – Günter Zöchbauer May 12 '16 at 10:19
  • I don't have really found an answer on my question. I want to get the name of the `route` and not of the component – Claudiu Matei May 12 '16 at 10:30
  • http://stackoverflow.com/a/37050883/217408 returns the route name. Otherwise please improve your question. Add code that demonstrates what you have tried, where you failed. I don't see where you want to get the current route name. In the `UserComponent`, in the root component, in a service, ... – Günter Zöchbauer May 12 '16 at 10:33
  • What should `this._router.name = "..."` Why would you assign a name to the `Router`? – Günter Zöchbauer May 12 '16 at 10:50
  • `path: '/add-user', name: 'AddUser', component: UserComponent` it is this `name` that I want to get – Claudiu Matei May 12 '16 at 10:51
  • That's what http://stackoverflow.com/a/37050883/217408 shows – Günter Zöchbauer May 12 '16 at 10:55
  • `_router.currentInstruction.component.routeName`did not worked, but `_router.root.currentInstruction.component.routeName` did the trick. Thank you! – Claudiu Matei May 12 '16 at 11:01
  • 1
    That depends on where your code is. In the root component you already get the root router and therefore no need for `root`. Glad to hear you figured it out :) – Günter Zöchbauer May 12 '16 at 11:09
  • 1
    I have printed the router so I could see al the properties and functions, and that's how I have seen how to get the routeName – Claudiu Matei May 12 '16 at 11:39

2 Answers2

2

What you are looking for is: router.currentInstruction.component.routeName

Small disclaimer: The new new router introduced in Angular 2.0 RC does not support route names. You might want to avoid using it.

Jean-Philippe Leclerc
  • 6,713
  • 5
  • 43
  • 66
  • So it is better to use `urlPath` instead of `routeName`. Because I will have to upgrade to the new version of Angular 2 – Claudiu Matei May 12 '16 at 18:46
2

>= Angular2 RC.0 <= RC.3

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());
  });
}

See also Angular2 get current route's alias

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567