如何在两个不同的角度模块中使用管道

use*_*723 4 javascript pipe typescript angular

我有一个烟斗

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

我有两个模块,我需要使用它.如果我在这两个模块中执行类似的操作,我会收到一条错误消息"两个模块声明了KeysPipe"

Module1,Module2:

declarations: [KeysPipe],
Run Code Online (Sandbox Code Playgroud)

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

@NgModule({
    declarations: [ KeysPipe],
})
export class KeysPipeModule {
}
Run Code Online (Sandbox Code Playgroud)

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

Module1,Module2:

imports: [KeysPipeModule],
Run Code Online (Sandbox Code Playgroud)

但现在我得到一个不同的模板错误,说找不到管道"无法找到管道'密钥'("v*ngIf ="docalc">"

Fil*_*auc 7

你走在正确的轨道上你的代码唯一缺少的是导出KeysPipeModule.它应该是这样的:

@NgModule({
    declarations: [ KeysPipe],
    exports: [KeysPipe]
})
export class KeysPipeModule {}
Run Code Online (Sandbox Code Playgroud)