从特征模块调用管道会产生错误

时间:2018-08-31 17:58:38

标签: angular

即使已在应用程序模块中导入并声明了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);
  }

}

1 个答案:

答案 0 :(得分:3)

在成角度的declarations类型— ComponentsDirectivesPipes中,只能由在Component内声明的Module使用。如果您在某个Pipe模块中使用Feature,那么还需要通过将declare模块中的该管道添加到feature数组中来declarations