2017-03-24 41 views
0

我有2個父組件和1個子組件。而我的項目的結構如下:Angular 2:類型ChildComponent是2個模塊聲明的一部分:parent1Module和Parent2Module

app 
    -> app.module.ts 
    -> app.component.ts 
    -> app.routing.module.ts 
parentComponent1 
    -> parent1.module.ts 
    -> parent1.component.ts 
parentComponent2 
    -> parent2.module.ts 
    -> parent2.component.ts 
childComponent 
    -> child.module.ts 
    -> child.component.ts 

現在我的問題是,當我使用的兒童及部件在parentComponent1和parentComponent2選擇爲:

<child-component [name]="parent1"></child-component> 

和導入子模塊中的兩個父組件爲:

parent1.module.ts--

@NgModule({ 
      imports:[ChildModule], 
      declarations:[ChildComponent] 
}) 
---- 
---- 
and so on.... 

以及在參數相同entComponent2的Moudle爲:

parent2.module.ts--

@NgModule({ 
       imports:[ChildModule], 
       declarations:[ChildComponent] 
    }) 
    ---- 
    ---- 
    and so on.... 

現在,當我開始我的項目,我的默認路由去parentComponent1並且在控制檯沒有錯誤,它工作正常,但是當我去parentComponent2,那麼錯誤出現在控制檯:

未處理的承諾拒絕:2個模塊 聲明的類型ChildComponent部分:Parent1Module和Parent2Module!請 考慮將ChildComponent移動到導入 Parent1Module和Parent2Module的更高模塊。您還可以創建一個新的NgModule ,出口和包括ChildComponent然後導入NgModule在 Parent1Module和Parent2Module

請幫我看看我在這裏做什麼錯。

+0

您在進口兒童霸租?在邏輯結構上似乎有錯誤。 – Amit

回答

7

使用此解決方案

在不止一個NgModule你不應該報關單的組件,你可以在一個NgModule導出,然後導入此NgModule其他NgModule。

child.module.ts

@NgModule({ 
    imports:[...], 
    declarations:[ChildComponent], 
    exports:[ChildComponent] 
}) 

parent1.module.ts

@NgModule({ 
    imports:[ChildModule], 
    declarations:[...] 
}) 

parent2.module.ts

@NgModule({ 
    imports:[ChildModule], 
    declarations:[...] 
}) 
+0

感謝Mr Tiep Phan提供的答案乾淨準確:)現在工作正常。 –

+0

很高興爲您效勞! –

+0

非常感謝!爲我工作。 –

相關問題