My angular router needs to use the same component for both the parent & child route, but pass along data to differentiate between the parent and child routes.
Current issue: the child route does not register any data from the sub-route.
Routing module segment that defines relationship:
{
path: 'parent-path/:id',
component: MyComponent,
children: [{
path: 'child-path'
component: MyComponent,
data: {MY_DATA_KEY: MY_DATA_VALUE},
}]
},
In the angular component:
constructor(
//...
private readonly activatedRoute: ActivatedRoute,
//...
) {}
ngOnInit() {
this.activatedRoute.data.subscribe(data => {
console.log('data', data); // This prints and empty object //
/* ... do something with data */
});
}
Current behaviour:
on hitting route '.../parent-path/some-id'
- normal expected behaviour, no data present
on hitting route '.../parent-path/some-id/child-path'
- unexpected behaviour, data is still empty
Note: I also tried adding data at the parent level, which does get registered at both routes. Relatively new to angluar, so any help would be appreciated. Thanks in advance!
MyComponent
used twice in the routing, is that correct? Also, have you tried usingthis.activatedRoute.snapshot.data
? – Vojtech