2017-05-25 73 views
0

我想在兒童路由中使用相同的數據,但是我找不到訪問父路由數據的方法。也就是說,現在我有:訪問來自兒童的角度父路由數據

{ path: 'parent-path', component: parentComponent, children: [ 
    { path: 'child1', component: child1Component, 
     data: { sharedData: 'sharedData', child1Data: 'child1Data' }}, 
    { path: 'child2', component: child2Component, 
     data: { sharedData: 'sharedData', child2Data: 'child2Data' }}, 
    { path: 'child3', component: child3Component, 
     data: { sharedData: 'sharedData', child3Data: 'child3Data' }}   
]} 

我想要的是不必在所有的孩子重複相同的sharedData。我試過這個:

{ path: 'parent-path', data: { sharedData: 'sharedData'}, component: 
    parentComponent, children: [ 
    { path: 'child1', component: child1Component, 
     data: { child1Data: 'child1Data' }}, 
    { path: 'child2', component: child2Component, 
     data: { child2Data: 'child2Data' }}, 
    { path: 'child3', component: child3Component, 
     data: { child3Data: 'child3Data' }}   
]} 

但是,通過這種方式,在每個組件中我只有他自己的數據(childXData)。

我怎麼會從孩子訪問父路由的數據?如果你需要觀察的route.parent.snapshot.data [「sharedData」]或不用快照:

回答

0

在孩子你可以注入ActivatedRoute(路線:ActivatedRoute),然後你可以做。