I'm trying to get the current route name or route path in my app.component when the route changes so that I can use the route name as a page class around a wrapper div. I'm trying to figure out the best way to handle this. Previously I was subscribing to the router changes property like I have listed below, but it seems like this is no longer available with @angular/router 3.0.0-alpha.3. If anyone has any thoughts or suggestions I would be most grateful.
this.router.changes.subscribe((val) => {
var path = this._location.path();
if (path.indexOf('page-name') !== -1) {
this.pageClass = 'page-name';
}
})
Steve's answer is the one documented currently, but the url observable on
ActivatedRoute
is not firing for me except when I first hit the base url (ex: going from /index to /index/item works, but going from /index/item to index/item/2, index/dashboard, or even /index does not).I'm not sure if the following solution is a best practice nor what the performance implications are, but I was able to get the active route on the
NavigationEnd
event of the router with the following:This fires every time you change the route, regardless of how deep you are in the url tree or where you navigate to/from.
This would probably help,
More details at https://angular.io/docs/ts/latest/guide/router.html