如何从父路由的组件访问激活的子路由数据? [英] How can I access an activated child route's data from the parent route's component?

查看:24
本文介绍了如何从父路由的组件访问激活的子路由数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 中的 ActivatedRoutedata.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆