[英]Angular 2 : Type ChildComponent is a part of the declarations of 2 modules : parent1Module and Parent2Module
我有2個父組件和1個子組件。 我的項目結構如下:
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....
並且在parentComponent2的Moudle中與:
parent2.module.ts--
@NgModule({
imports:[ChildModule],
declarations:[ChildComponent]
})
----
----
and so on....
現在,當我啟動我的項目時,我的默認路由轉到了parentComponent1,並且控制台中沒有錯誤,並且運行正常,但是當我轉到parentComponent2時,該錯誤在控制台中顯示為:
未處理的承諾拒絕:在2個模塊的聲明中鍵入ChildComponent部分:Parent1Module和Parent2Module! 請考慮將ChildComponent移到導入Parent1Module和Parent2Module的更高模塊。 您還可以創建一個新的NgModule,該導出並包含ChildComponent,然后在Parent1Module和Parent2Module中導入該NgModule
。
請幫我解決我在這里做錯了什么。
使用此解決方案
您不應在一個以上的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:[...]
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.