Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Get Route Parameters from parent route

I have a global routing module which allows me to correctly navigate to my component module which is lazy loaded.

Inside the component module I have a child route which can be correctly loaded. What I can't seem to do however is access the userID parameter from inside the albums path (the child of the userDetailComponent)

I've tried the following, but it returns as undefined.

this.activatedRoute.params.subscribe((urlParameters) => {
  this.user_id = urlParameters['userID'];
}

Below is my routing configuration. Any help would be greatly appreciated.

app-routing.module.ts

const routes: Routes = [
  { 
     path: 'users', 
     loadChildren: '../app/users/users.module#UsersModule' 
  },
  {
     path: 'users/:userID',
     loadChildren: '../app/component/component.module#ComponentModule' 
   }
]

component.module.ts

const routes: Routes = [
  {
    path: '',
    component: UserDetailComponent,
    children: [
      {
        path: 'albums',
        loadChildren: '../app/albums/albums.module#AlbumsModule';
      },
    ]
  },
]

1 Answers

albums path is child of UserDetailComponent's route, and UserDetailComponent's route is child of users/:userId. So when you are inside albums route, you should get params of UserDetailComponent's route by routing tree. You can access parent of your route using ActivatedRoute.parent (it returns route which is parent of your current route)

like image 135
web.dev Avatar answered Oct 26 '25 08:10

web.dev



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!