【问题标题】:Angular 5 not able to find custom pipeAngular 5 找不到自定义管道
【发布时间】:2018-11-25 14:12:16
【问题描述】:

我能找到的每一个地方都说你只需要在模块文件中声明它我错过了什么?如果有人需要更多信息,我可以添加任何需要的信息

管道文件:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'partnersearch'
})
export class PartnerPipe implements PipeTransform {

    transform(value: any, args?: any): any {
        if (value.startsWith("::ffff:")) value = value.slice(7);
        return value;
    }

}

module.ts 文件(它们在同一个文件夹中):

import { PartnerPipe } from './partner.pipe';

@NgModule({
imports: [],
declarations: [
    PartnerPipe
]})

html:

{{ partner | partnersearch }}

【问题讨论】:

标签: angular pipe


【解决方案1】:

您需要在模块中声明它,以便组件可以使用它。

import { NgModule } from '@angular/core';
import { PartnerPipe } from './partner.pipe';

@NgModule({
  declarations: [PartnerPipe],
})
export class MyModule {}

【讨论】:

  • @amedeiros 所以一定要发布所有需要的东西
【解决方案2】:

如果它在共享模块中,您也应该将其导出。

import { PartnerPipe } from './partner.pipe';

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

【讨论】: