Angular找不到我的管道,即使它是在应用程序模块中导入和声明的。
应用模块ts
import { PercentSavedPipe } from './shared/pipes/percent-saved.pipe';
@NgModule({
declarations: [
PercentSavedPipe
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
从功能模块的模板调用管道:
<small class="ml-1">
({{product.price | percentSaved:product.list_price |
currency:'USD':'symbol-narrow'}}%)
</small>
产生以下错误:
ERROR Error: Uncaught (in promise): Error: Template parse errors:
The pipe 'percentSaved' could not be found ("</small>
<small class="ml-1">
[ERROR ->]({{product.price | percentSaved:product.list_price |"):
ng:///BrandModule/BrandComponent.html@167:3
Error: Template parse errors:
The pipe 'percentSaved' could not be found ("</small>
<small class="ml-1">
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'percentSaved'
})
export class PercentSavedPipe implements PipeTransform {
transform(price: number, list_price: number): number {
return 100 - (100 * price / list_price);
}
}