9

I want to declare pipe in parent module and use it in child modules.

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

How can I use It child modules?

Because if I declare it twice I got error

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.

Matthew Green
  • 10,161
  • 4
  • 36
  • 54
John Doe
  • 3,794
  • 9
  • 40
  • 72

2 Answers2

11

You need to create another module where you put the pipe and then import that module where you want to use that pipe.

One directive, component, or pipe can always belong only to exact one NgModule but this NgModule can be imported to as many modules as desired.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Is it possible to use parents declarations from child modules? – John Doe Sep 29 '16 at 16:35
  • No, you have to import all modules you want to use declarations from directly. You can put several reusable components, directives and pipes together into an NgModule. You can also create an NgModule (A) that export other NgModules (B, C, D) so that you can import B, C, D by just adding A to imports of your current module. – Günter Zöchbauer Sep 29 '16 at 16:38
0

As Günter Zöchbauer answer, Find Example here

Angular 2 - Pipe reuse in multiple modules - error not found or duplicate definition

Yatin Patel
  • 512
  • 7
  • 17