【问题标题】:How to use a pipe in two different Angular Modules如何在两个不同的 Angular 模块中使用管道
【发布时间】:2017-01-12 05:45:37
【问题描述】:

我有一个管道

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
  .....
    return keys;
  }
}

我有两个模块需要使用它。如果我在两个模块中都这样做,我会收到一条错误消息,说“两个模块声明 KeysPipe”

模块1、模块2:

declarations: [KeysPipe],

然后我尝试通过它自己的模块导出 KeysPipe,以便我可以将它导入到我需要使用它的两个模块中

@NgModule({
    declarations: [ KeysPipe],
})
export class KeysPipeModule {
}

现在我在需要使用 KeysPipe 的两个模块中导入 KeysPipeModule

模块1、模块2:

imports: [KeysPipeModule],

但现在我收到一个不同的模板错误,提示找不到管道“找不到管道'键'(“v *ngIf="docalc">”

【问题讨论】:

    标签: javascript angular typescript pipe


    【解决方案1】:

    您在正确的轨道上,您的代码唯一缺少的是KeysPipeModule 中的导出。它应该是这样的:

    @NgModule({
        declarations: [ KeysPipe],
        exports: [KeysPipe]
    })
    export class KeysPipeModule {}
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多