即使已在应用程序模块中导入并声明了Angular,也找不到我的管道。
app.module.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">
saved.pipe.ts
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);
}
}
答案 0 :(得分:3)
在成角度的declarations
类型— Components
,Directives
和Pipes
中,只能由在Component
内声明的Module
使用。如果您在某个Pipe
模块中使用Feature
,那么还需要通过将declare
模块中的该管道添加到feature
数组中来declarations
。