如何从父路线的组件访问已激活的子路线的数据? [英] How can I access an activated child route's data from the parent route's component?
本文介绍了如何从父路线的组件访问已激活的子路线的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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' }
]}
];
如果我导航到/parent/child2
,然后从parentComp
看ActivatedRoute
,则定义了data.foo
,但未定义data.bar
.我可以访问所有孩子的数组,但是我不知道哪个孩子被激活了.
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
constructor(route: ActivatedRoute) {
route.url.subscribe(() => {
console.log(route.snapshot.firstChild.data);
});
}
这篇关于如何从父路线的组件访问已激活的子路线的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文