Angular custom pipe not be found

*爱你&永不变心* 提交于 2019-12-03 21:30:46

问题


In my application I need a custom pipe globally, I try to implement it following angular pipe but i see always this error

Template parse errors: The pipe 'formatdate' could not be found

formatdate.pipe

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

@Pipe({
  name: 'formatdate'
})

export class FormatdatePipe implements PipeTransform {

  transform(dateJson: any, args?: any): any {
.
 //code...
.
      return dateJson;
    }
  }
}

app.module

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';
@NgModule({
  declarations: [
    AppComponent, FormatdatePipe 
  ],

This pipe works if I import it in all my module and not in the principal app.module, do I need a routin pipe module or something


回答1:


Pipes (like Components and Directives) don't work globally like services do.

You need to define a pipe in some module. Then you can use it in components defined in that module. Another way is to add the pipe to exports of a module and then import that module in the module where you want to use it.

Define it like this:

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';

@NgModule({
  declarations: [
    FormatdatePipe 
  ],
  exports: [
    FormatdatePipe
  ]
})   
export class SomeUtilModule {}

Then import this module where you want to use it and it should work :)



来源:https://stackoverflow.com/questions/45098278/angular-custom-pipe-not-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!