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

后端 未结 1 1309
忘了有多久
忘了有多久 2020-12-25 11:06

Im working on angular 2 final release.

I have declared two modules: main app and one for the settings page.

相关标签:
1条回答
  • 2020-12-25 12:12

    If you want to use the pipe in a different module, then add the module where the pipe is declared to imports: [...] of the module where you want to re-use the pipe, instead of adding it to declarations: [] of multiple modules.

    For example:

    @NgModule({
        imports: [],
        declarations: [JsonStringifyPipe],
        exports: [JsonStringifyPipe]
    })
    export class JsonStringifyModule { }
    
    @NgModule({
        imports: [
          BrowserModule, HttpModule, routing, FormsModule, SettingsModule,
          JsonStringifyModule],
        declarations: [AppComponent],
        bootstrap: [AppComponent]
    })
    export class AppModule { }
    
    @NgModule({
        imports: [
           CommonModule, HttpModule, FormsModule, routing, 
           JsonStringifyModule],
        declarations: [SettingsComponent],
        exports: [SettingsComponent],
        providers: []
    })
    export class SettingsModule { }
    
    0 讨论(0)
提交回复
热议问题