将管道移至共享模块时解析错误

时间:2019-05-06 05:04:00

标签: angular

当我尝试将此声明移至共享模块时(因为我需要在许多组件中使用此管道),我有一个在组件模块中声明时成功运行的管道。我在HTML中收到一个解析错误,指示找不到管道。

管道

import { Pipe, PipeTransform, NgModule } from '@angular/core';
import { Relations } from '../helpers/relations';

@Pipe({
    name: 'lookupRelation'
})
@NgModule()
export class LookupRelationPipe implements PipeTransform {
    transform(value: any, args?: any ): any {
        return new Relations().getType(value);
    }
}

共享模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { LookupRelationPipe } from '../pipes/lookup.relation.pipe';

@NgModule({
  imports: [
      CommonModule
  ],
  declarations: [ 
      LookupRelationPipe
  ],
  exports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    LookupRelationPipe
  ] 
})
export class SharedModule { }

组件模块

import { NgModule } from '@angular/core';

import { BeneficiaryRoutingModule } from './beneficiary-routing.module';
import { BeneficiaryListComponent } from './beneficiary-list/beneficiary-list.component';
import { AngularMaterialModule } from '../angular-materials.module';

import { BeneficiaryDialog } from './beneficiary.dialog/beneficiary.dialog';
import { LookupContacttypePipe } from '../pipes/lookup-contacttype.pipe';
import { SharedModule } from '../shared/shared.module';
//import { LookupRelationPipe } from '../pipes/lookup.relation.pipe'; 

@NgModule({
  declarations: [
    BeneficiaryListComponent, 
    BeneficiaryDialog, 
    LookupContacttypePipe
  //  LookupRelationPipe
  ],
  imports: [
    BeneficiaryRoutingModule,
    AngularMaterialModule,
    SharedModule
  ],
  providers: [],
  entryComponents: [BeneficiaryDialog]
})
export class BeneficiaryModule { }

注意:当我取消注释此模块中的管道减速时,该应用程序将成功执行。

html

...
           <ng-container matColumnDef="relation">
                <th mat-header-cell *matHeaderCellDef>Relation</th>
                <td mat-cell *matCellDef="let element">{{ element.relation | lookupRelation }}</td>
              </ng-container>

....

我收到一个解析错误,指示将声明移到共享模块时找不到管道,如果在组件模块中声明该管道,则在管道中成功执行。还应该注意的是,当我将CommonModule和forms模块移到同一共享模块(并从组件模块中删除了导入)时,它们可以正常工作。

0 个答案:

没有答案