代码之家  ›  专栏  ›  技术社区  ›  koque

从功能模块对管道的角度调用产生错误

  •  0
  • koque  · 技术社区  · 8 年前

    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);
      }
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   SiddAjmera    8 年前

    棱角分明的 declarations Components , Directives Pipes 只能由使用 Component 他在里面声明 Module . 如果你正在使用 Pipe 在某些方面 Feature 那么你还需要 declare 那根管子 feature 通过将其添加到 声明

    推荐文章