Struggling to access the parent route params in a lazy loaded route component using activatedRoute.parent.params
. Despite this not working, I have managed to find a solution that involves fetching the value using an array index number which feels like a 'magic' workaround.
Is there a more elegant method available that doesn't require retrieving the value through an array (
this.activatedRoute.pathFromRoot[1]
)?
The route setup for lazy loading a module looks like this:
parent.route
routes:Routes = [
{
path: "dashboard/:id",
component: dashboard.DashboardComponent,
canActivate: [guard.LoggedInGuard],
children: [
{
path: "my-dock",
loadChildren: 'path/child.module#Child'
}
]
}
];
In the default component for child.module
, the code is as follows:
child.component
ngOnInit() {
this.activatedRoute.pathFromRoot[1].params.subscribe((params: Params) => {
console.log(params); // returns {id: "123456789"}
});
this.activatedRoute.parent.params.subscribe((params: Params) => {
console.log(params); // returns {}
});
}
Wondering why
this.activatedRoute.parent.params
does not work to retrieve the id
param?