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)?
{ path: '/about', as: 'About', component: About }
Is it possible?
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)?
{ path: '/about', as: 'About', component: About }
Is it possible?
NOTE: The following has been tested with the 2.0 beta series. The RC versions have an updated router component with breaking changes. The old one has been renamed to
router-deprecated
. This has not been tested yet against the new router.The following will print
Foo
orBar
depending on your active route.You should not use location, instead you should use Router instance.
In your component, inject it in constructor with:
than, you could obtain name of component with:
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
If you know the list of possible aliases then it's possible to find out the name of the current alias using a combination of
router.generate
androute.isActiveRoute
:For example, I have a wizard with three steps. I have a list of step aliases in an array:
I then use the following code to determine the current alias name:
Disclaimer: I haven't tried to see if this works with route parameters.
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
or the full path using
or
Can't see a way of getting it, but one alternative is to use RouteData to pass the alias of the route
https://angular.io/docs/ts/latest/api/router/RouteData-class.html