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
or Bar
depending on your active route.
@Component({
selector: 'app',
templateUrl: 'app/app.html',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path:'/', name: 'Foo', component: FooComponent, useAsDefault: true},
{path:'/bar', name: 'Bar', component: BarComponent, useAsDefault: false},
])
export class AppComponent implements OnInit {
constructor(private _router: Router) {
}
ngOnInit() {
console.log('current route name',
this._router.currentInstruction.component.routeName);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With