我有组件基础 angular4 应用程序,并且在每个组件中我都使用DataTable Plugin 和自定义DataFilterPipe,当我在每个组件中像下面这样导入时,我得到的错误是ERROR Error: Uncaught (in promise): Error: Type DataFilterPipe is part of the 2个模块的声明。请考虑将 DataFilterPipe 移至更高的模块。然后我创建了一个名为dataFilterModule的新模块
进口:
import {DataTableModule} from "angular2-datatable";
import {DataFilterPipe} from "../plugins/datatable/datafilterpipe";
import {HttpModule} from "@angular/http";
import {FormsModule} from "@angular/forms";
数据过滤模块:
import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {DataTableModule} from "angular2-datatable";
import {DataFilterPipe} from "../plugins/datatable/datafilterpipe";
import {HttpModule} from "@angular/http";
import {FormsModule} from "@angular/forms";
@NgModule({
imports: [
CommonModule,
DataTableModule,
FormsModule,
HttpModule
],
declarations: [
DataFilterPipe
],
exports: [
DataFilterPipe
]
})
export class dataFilterModule {}
然后在我的AppModule中导入这个模块但是!!我得到的错误是:
The pipe 'dataFilter' could not be found
我添加了如下导出的 dataFilterModule 但没有任何好消息!
export class dataFilterModule {
static forRoot() {
return {
ngModule: DataFilterPipe,
providers: [],
};
}
}
编辑: 我的自定义过滤器:
import * as _ from 'lodash';
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'dataFilter'
})
export class DataFilterPipe implements PipeTransform {
transform(array: any[], query: string): any {
if (query) {
return _.filter(array, row=>row.name.indexOf(query) > -1);
}
return array;
}
}