const appRoutes: Routes = [
{ path: 'parent', component: parentComp, data: { foo: 'parent data' }, children: [
{ path: 'child1', component: childComp1, data: { bar: 'child data 1' },
{ path: 'child2', component: childComp2, data: { bar: 'child data 2' }
]}
];
If I navigate to /parent/child2
and then look at the ActivatedRoute
from parentComp
, data.foo
is defined, but data.bar
is not. I have access to an array of all the children, but I don't know which one is activated.
How can I access the activated child route's data from a parent route's component?
First child will give you access to data
Working with Angular 6, I managed to get the current route data from a parent component, with the following code:
I've configured the Router with the Extra options to inherit the parent routes data:
Then in my parent component, I was able to see the data changes with:
Enjoy!