2017-03-01 51 views
0

我有一個帶有路由器插座的組件。這個組件有一個select元素,我通過服務在ngOnInit上提供了一些類別。有3條子路線,其中一條導航到一個也希望使用這些類別的組件。角度2:將數據從父項傳輸到子項

我的路線是這樣的

const routes: Routes = [ 
{ 
    path: 'marketplace', 
    component: MarketplaceComponent, 
    resolve: { categories: CategoriesResolveService }, 
    canActivate: [AuthenticatedGuard], 
    children: [  
    { path: 'modules', component: ModulesComponent, resolve: { categories: CategoriesResolveService } }  
    ] 
}]; 

這是使2 HTTP調用獲得類別。我怎樣才能將這些數據傳輸給子組件?我想避免再做一次http調用。是否有可能在父母和孩子之間分享這些數據?

+0

你嘗試用viewChild()? –

+0

不知道viewChild是什麼,會看一看,謝謝 –

+0

ViewChild通過在父模板中聲明子組件來工作?如果是這樣,那麼我認爲它不會在這種情況下工作,我只有路由器插座。 –

回答

0
export class parentComponent { 

    @ViewChild(ChildOneComponent) 
    private childOneComponent: ChildOneComponent; 

    @ViewChild(ChildTwoComponent) 
    private childTwoComponent: ChildTwoComponent; 

    let date = this.childOneComponent.method1; 

} 

這種方法1()是定義你的孩子組件,用於獲取method.you必須在父組件導入子組件。

ChildOneComponent { 
    get method1() { 
    return this.value//return which value you pass. 
    } 
} 
相關問題