Javascript Angular2从应用程序根目录中的子路由获取数据

Javascript Angular2从应用程序根目录中的子路由获取数据,javascript,angular,angular2-routing,Javascript,Angular,Angular2 Routing,在我的应用程序根组件中,我有一些样式的路由器插座。 我有一条路线: { path: 'some-path', component: ChildContainer, data: { variable:'variable' }, } 我可以在ChildContainer中获取变量,但我需要它在Approt中。因此,从中,我可以从child中获得它,但如果我在Approt构造函数中这样做: const state: RouterState = router.routerSta

在我的应用程序根组件中,我有一些样式的
路由器插座
。 我有一条路线:

{
    path: 'some-path',
    component: ChildContainer,
    data: { variable:'variable' },
}
我可以在ChildContainer中获取变量,但我需要它在Approt中。因此,从中,我可以从
child
中获得它,但如果我在Approt构造函数中这样做:

const state: RouterState = router.routerState;
const root: ActivatedRoute = state.root;
const child = root.firstChild;
console.log(root,child)
-child为null,并且root包含正确的子级(invoke属性getter)。
那么,如何在Approt中获取
变量

您可以点击激活事件以获取路由器出口内实例化组件的引用

检查这个


您可以点击激活事件以获取路由器出口内实例化组件的引用

检查这个


非常感谢,但情况有点不同。在我的中,我需要从路由器数据@qweasd获取变量:根据您的评论更新答案,干杯!!非常感谢,但情况有点不同。在我的中,我需要从路由器数据@qweasd获取变量:根据您的评论更新答案,干杯!!
@Component({
  selector: 'my-app',
  template: `<h3 class="title">Basic Angular 2</h3>
  <router-outlet (activate)="onActivate($event)" ></router-outlet>
  `
})
export class AppComponent {
  constructor(){}

  onActivate(componentRef){
    componentRef.sayhello();
  }
}

@Component({
  selector: 'my-app',
  template: `<h3 class="title">Dashboard</h3>
  `
})
export class DashboardComponent {
  constructor(){}

  sayhello(){
    console.log('hello!!');
  }
}
onActivate(componentRef){
    componentRef.route.data.subsribe(data => {
       console.log(data);
    });
}