Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular get current active component path

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.

like image 871
fastAsTortoise Avatar asked Aug 15 '26 09:08

fastAsTortoise


1 Answers

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

like image 117
fastAsTortoise Avatar answered Aug 18 '26 01:08

fastAsTortoise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!