简体   繁体   中英

Angular 2 RC.5 Shared Module can't find pipes

I'm updating my app to use a module structure and I ran into a weird issue when trying to add my pipe component into a shared module. From what I've read I have everything set up right, so I must be missing something little.

Error: Unhandled Promise rejection: Template parse errors: The pipe 'cmgTitleize' could not be found

I have a BrowseModule , this module declares a ProjectCardComponent which has a template that uses the cmgTitleize pipe. To provide access to the TitleizePipe I import my SharedModule .

@NgModule({
  declarations: [
    ...,
    ProjectCardComponent
  ],
  imports: [
    ...,
    SharedModule
  ],
  providers: [
    ...
  ]
})

export class BrowseModule { }

The SharedModule , imports the PipesModule :

@NgModule({
  declarations: [
    ...
  ],
  exports: [
    ...
  ],
  imports: [
    ...,
    PipesModule
  ]
})

export class SharedModule { }

PipesModule declares and exports the TitelizePipe :

@NgModule({
  declarations: [
    ...
    TitleizePipe
  ],
  exports: [
    ...
    TitleizePipe
  ]
})

export class PipesModule { }

Lastly for a sanity check heres the TitleizePipe:

@Pipe({
  name: 'cmgTitleize'
})

export class TitleizePipe implements PipeTransform {
  ...
}

看起来我只需要在PipesModule中导出SharedModule

If you're using the static "forRoot()" in a shared module, you still need to export the pipes or any other required module:

@NgModule({
    exports: [
        MyPipesModule                   //<---------------- HERE
    ]
})
export class SharedModule { 
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: SharedModule,
            providers: [
                MySingletonServices
            ]
        };
    }
}

And then you just import it in your main app module:

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        SharedModule.forRoot()       // <------------
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM