0

我有组件基础 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;
  }
 }
4

2 回答 2

2

答案是共享模块。查看我几天前回答的类似帖子。

于 2017-07-25T04:31:16.163 回答
1

检查@joshrathke joshrathke 答案后,我找到了解决方案。您应该创建ShareModule然后导入您的指令、组件或管道并使用它。你应该在你需要的每个组件中导入ShareModule !

于 2017-07-25T05:15:56.957 回答