2017-08-08 76 views
1

我有一個懶加載模塊,需要使用一個組件。我已經將它添加到應用程序模塊聲明中,並期望這應該使聲明的組件應用程序範圍更廣。延遲加載模塊如何繼承應用程序模塊聲明?

儘管Angular無法識別延遲加載的模塊中的組件。我試着將它添加到兩個模塊聲明中,然後得到一個警告,要求在app.modulelazyLoaded.module之上的更高模塊中添加聲明。

例如(nameOfModule/Component

Error: Type (DeclareMeComponent) is part of the declarations of 2 modules: (AppModule) and (LazyLoadedModule)! Please consider moving (DeclareMeComponent) to a higher module that imports AppModule and (LazyLoadedModule). You can also create a new NgModule that exports and includes (DeclareMeComponent) then import that NgModule in (AppModule) and (DeclareMeComponent).

什麼是app.module?我怎樣才能得到延遲加載模塊繼承/使用聲明的組件連同應用模塊?

+0

創建共享模塊的最後一句話意味着進口。 https://angular.io/guide/ngmodule#shared-modules – echonax

+0

好的,謝謝你的建議 – Jonathan002

回答

2

高於AppModule將是SharedModule。在這裏,您可以定義在AppModule和任何惰性加載模塊中將使用的組件。

@NgModule({ 
    imports: [ 
    CommonModule 
    ], 
    declarations: [ 
    DeclareMeComponent 
    ], 
    exports: [ 
    DeclareMeComponent, 
    CommonModule 
    ]  
}) 
export class SharedModule {} 

在這裏,您將聲明和導出組件,指令,管道,整個shebang。您將在整個應用程序中使用的核心構建塊。在這裏你可能還會導入和導出CommonModule。它就像BrowserModule,但是你可以在應用程序中多次導入。

您現在可以在AppModule及其他任何模塊中通過導入它來使用您的共享組件。

@NgModule({ 
    imports: [ 
    BrowserModule, 
    SharedModule 
    ] 
}) 
export class AppModule {} 

正如你所看到的,沒有必要宣佈DeclareMeComponentAppModule裏面,因爲它是從更高SharedModule

+0

感謝你! :) – Jonathan002