问题
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