【问题标题】:Angular 2 RC.5 Shared Module can't find pipesAngular 2 RC.5 共享模块找不到管道
【发布时间】:2017-01-10 00:16:01
【问题描述】:

我正在更新我的应用程序以使用模块结构,但在尝试将管道组件添加到共享模块时遇到了一个奇怪的问题。从我读到的内容来看,我的一切设置都是正确的,所以我一定遗漏了一些东西。

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

我有一个BrowseModule,这个模块声明了一个ProjectCardComponent,它有一个使用cmgTitleize 管道的模板。为了提供对TitleizePipe 的访问权限,我导入了我的SharedModule

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

export class BrowseModule { }

SharedModule,导入PipesModule

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

export class SharedModule { }

PipesModule 声明并导出TitelizePipe

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

export class PipesModule { }

最后,TitleizePipe 进行完整性检查:

@Pipe({
  name: 'cmgTitleize'
})

export class TitleizePipe implements PipeTransform {
  ...
}

【问题讨论】:

    标签: angular angular-module


    【解决方案1】:

    如果您在共享模块中使用静态“forRoot()”,您仍然需要导出管道或任何其他所需模块:

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

    然后您只需将其导入您的主应用模块:

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

    【讨论】:

    • .forRoot() 方法是否仍然适用于 Angular 4+?我听说它已被弃用?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2018-11-25
      • 1970-01-01
      • 2016-12-06
      相关资源
      最近更新 更多