3

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';
      },
    ]
  },
]
Que
  • 957
  • 2
  • 14
  • 35
  • Check this. https://stackoverflow.com/questions/35688084/how-to-get-query-params-from-url-in-angular-2 – Shubham Chaudhary Feb 22 '19 at 09:18
  • Thanks Shubham - I had already seen that one and it unfortunately didn't work. – Que Feb 22 '19 at 09:23
  • I ended up just creating a service from the user detail component which updates the service with the route param i need, and then I access it from there. – Que Feb 22 '19 at 09:40
  • @ShubhamChaudhary that was query params, he needs route params. – Al-Mothafar May 27 '19 at 04:52
  • 1
    Possible duplicate of [How to get params url parent id from children](https://stackoverflow.com/questions/49349498/how-to-get-params-url-parent-id-from-children) – developer033 Sep 16 '19 at 02:22

1 Answers1

3

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)

web.dev
  • 349
  • 1
  • 6
  • Not really! thats not true, because of the lazy load seems, but I got full path and the paramsMap only return with the one for the child without the parent one – Al-Mothafar May 27 '19 at 04:51