2016-11-22 57 views
3

我正在開發一個使用Angular JS 2.0ASP.net MVC的大型應用程序。 我在我的申請大約爲120組件,這些組件在@ngModule聲明塊中的所有聲明:在@ngModule Angular 2中聲明多個組件

@NgModule({ 
    imports: [CommonModule], 
    declarations: [Component1, Component2, Component3, ..., Component120] 
}) 

是否有一個機制,我可以縮短這個?

+0

擊穿你的應用程序更小的小'模塊/ submodule',覺得以這樣的方式,每個模塊不應該有比'10-15'成分越多,你可以爲每個'submodule'及負載創建NgModule他們懶惰地通過路由器功能(這也可以增加應用程序性能)。 –

回答

2

您可以構建共享模塊並將組件添加到此模塊,那麼您只需將共享模塊添加到imports: [MySharedModule],並且MySharedModule導出的所有組件和指令可供導入模塊使用。

@NgModule({ 
    imports: [ CommonModule ], 
    declarations: [ Component1, Component2, Component3....  Component120 
], 
    imports: [ CommonModule ], 
    exports: [ Component1, Component2, Component3....  Component120 ], 
}) 
class MySharedModule {} 

@NgModule({ 
    declarations: [AppComponent], 
    bootstrap: [AppComponent], 
    imports: [BrowserModule, MySharedModule] 
}) 
class AppModule {} 
+0

感謝您的建議,將以這種方式實施。 – shabari

+0

再次感謝,它爲我工作:) – shabari

0

恐怕沒有更簡單的方法來實現你正在嘗試做的事情。

但是,如果可能的話,你可以將應用程序分成多個部分。這意味着您將擁有多個app.module.ts。

相關問題