I have following routes and I am trying to get the path of the children component.
const myRoutes: Routes = [
{
path: '',
component: mainComponent
}, {
path: ':id/create',
component: anothercomponent,
children: [
{ path: '', redirectTo : 'general-information', pathMatch: 'full' },
{ path: 'general-information', component: GeneralInfoComponent},
{ path: 'another-info', component: AnotherInfoComponent}
]
}]
To get current path segment on reload i am doing something like this
route.firstChild.url.subscribe(val => {
let myRoute = val[0].path;
});
Even though i am subscribing to url (in the anotherComponent) i am not getting the triggers if child route changes under :id/create i.e if it changes from 022/create/general-information to 022/create/another-info and to get every segment change for child routes i have to do something like
router.events.subscribe(event:Event => {
if(event instanceof NavigationEnd) {
this.route.firstChild.url.subscribe(val => {
let myroute = val[0].path;
});
}
})
Is there any easier way to get the path only ? like just general-information or another-info ? If yes please provide the solution and if not what is the reason. Hope i explained it nicely but let me know if need more clarification. Thanks in advance.
year later since no on answered you can get it like:
this._activatedRoute.snapshot.routeConfig.path
if you are on route let say /dashboard/graphs this will give you graphs
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