我有自定义管道。我导入到组件的ngModule。
declarations: [SearchFilterPipe]
在这个组件中,我尝试将此管道用作:
<div *ngFor="let item of studyPlan | searchfilter : value">
它给了我一个错误:The pipe 'searchfilter' could not be found
。
此行中也没有变量value
。
管道是:
import { Injectable, Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'searchfilter'
})
@Injectable()
export class SearchFilterPipe implements PipeTransform {
transform(items: any[], field: string, value: string): any[] {
if (!items) { return []; }
return items.filter(it => it[field] === value);
}
}
组件模块是:
@NgModule({
...
declarations: [SearchFilterPipe],
exports: [SearchFilterPipe]
})
答案 0 :(得分:2)
要么
@Pipe({name: 'searchfilter'})
装饰器与declarations: [SearchFilterPipe]
与包含<div *ngFor="let item of studyPlan | searchfilter : value">
imports: [TheModuleContainingThePipe]
会丢失。exports: [SearchFilterPipe]
中缺少@NgModule()
。