简体   繁体   English

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

[英]How to use a pipe in two different Angular Modules

I have a pipe 我有一个烟斗

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

I have two modules in which I need to use this. 我有两个模块,我需要使用它。 If I do something like this in both the modules, I get an error saying that "two modules declare KeysPipe" 如果我在这两个模块中执行类似的操作,我会收到一条错误消息“两个模块声明了KeysPipe”

Module1, Module2: Module1,Module2:

declarations: [KeysPipe],

I then tried exporting KeysPipe through it's own module so that I can import it in to the two modules in which I need to use it 然后我尝试通过它自己的模块导出KeysPipe,以便我可以将它导入到我需要使用它的两个模块中

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

Now I'm importing the KeysPipeModule in the two modules I need to use KeysPipe 现在我在我需要使用KeysPipe的两个模块中导入KeysPipeModule

Module1, Module2: Module1,Module2:

imports: [KeysPipeModule],

But now I get a different template error saying that the pipe isn't found "The pipe 'keys' could not be found ("v *ngIf="docalc">" 但现在我得到一个不同的模板错误,说找不到管道“无法找到管道'密钥'(”v * ngIf =“docalc”>“

You're on the right track the only thing your code is missing is the export in the KeysPipeModule . 您在正确的轨道上,您唯一缺少的代码是KeysPipeModule中的KeysPipeModule This is what it should look like: 它应该是这样的:

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

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

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