简体   繁体   English

Angular 2 RC.5共享模块找不到管道

[英]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 错误: 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. 我有一个BrowseModule ,这个模块声明一个ProjectCardComponent ,它有一个使用cmgTitleize管道的模板。 To provide access to the TitleizePipe I import my SharedModule . 为了提供对TitleizePipe访问,我导入了我的SharedModule

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

export class BrowseModule { }

The SharedModule , imports the PipesModule : SharedModule ,导入PipesModule

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

export class SharedModule { }

PipesModule declares and exports the TitelizePipe : PipesModule声明并导出TitelizePipe

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

export class PipesModule { }

Lastly for a sanity check heres the TitleizePipe: 最后,为了进行理智检查,继承了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: 如果您在共享模块中使用静态“forRoot()”,则仍需要导出管道或任何其他所需模块:

@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 { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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