2016-09-29 26 views
5

我想在父模塊中聲明管道並在子模塊中使用它。在子模塊中使用相同的管道組件聲明問題

@NgModule({ 
    // Pipe which I want to declare in all child modules 
    declarations: [ ThisIsPipe ], 
    imports: [ ChildModuleOne, ChildModuleTwo], 
}) 

如何使用它的子模塊?

因爲如果我聲明它兩次我有錯誤

Uncaught Error: Type ThisIsPipe is part of the declarations of 2 modules: ChildModuleOne and ChildModuleTwo! Please consider moving ThisIsPipe to a higher module that imports ChildModuleOne and ChildModuleTwo. You can also create a new NgModule that exports and includes ThisIsPipe then imports that NgModule in ChildModuleOne and ChildModuleTwo.

回答

6

您需要創建你把管道中,然後在要使用管道導入模塊,其他模塊。

一個指令,組件或管道總是隻能屬於一個NgModule,但是這個NgModule可以根據需要導入到任意數量的模塊。

+0

是否可以使用父母的聲明從子模塊? –

+0

不,您必須直接導入要使用聲明的所有模塊。你可以將幾個可重用的組件,指令和管道放在一起成爲一個NgModule。您還可以創建一個NgModule(A),導出其他NgModules(B,C,D),以便通過將A添加到當前模塊的導入來導入B,C,D。 –

相關問題