【问题标题】:Angular - No pipe found with nameAngular - 找不到名称的管道
【发布时间】:2020-09-21 11:10:25
【问题描述】:

我使用“ng g pipe”命令创建了一个管道。在我的代码中使用它时出现控制台错误。 下面附上代码截图。 错误:错误 NG8004:找不到名为“filterByPrcName”的管道filter-by-prc-name.pipe.tsConsole Error Message product-list.component.html

【问题讨论】:

  • 您需要将其添加到模块的声明数组中。如果你想将它暴露给导入它声明的模块的其他模块,你还需要将它添加到导出数组中。
  • 谢谢@JasonWhite。我错过了导出它。现在一切正常。

标签: angular angular8 angular9 web-frontend angular-pipe


【解决方案1】:

您需要打开声明组件的 Angular 模块,然后将其添加到声明中,并添加所需的导入。

例子:

 <td>{{product.productCode | lowercase | convertToSpaces: '-' }}</td>

src/app/products/product-list.component.html:48:61 中的错误 - 错误 NG8004:找不到名称为“convertToSpaces”的管道。

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { ProductListComponent } from './products/product-list.component';
import { ConvertToSpacesPipe } from './shared/convert-to-spaces.pipe'; // <-- Add this

@NgModule({
  declarations: [
    AppComponent,
    ProductListComponent,
    ConvertToSpacesPipe  // <-- Add this
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  bootstrap: [AppComponent],

  //exports: [AppComponent],

})
export class AppModule { }

convert-to-spaces.pipe.ts

import { Pipe, PipeTransform } from '@angular/core'

@Pipe({ name: 'convertToSpaces' })

export class ConvertToSpacesPipe implements PipeTransform {

    transform(value: string, character: string): string {
        return value.replace(character, ' ');
    }

}

【讨论】:

  • 不适合我使用 ionic 4 实现
【解决方案2】:

如果您的组件不在其父模块的声明数组中,您也会收到此错误。

【讨论】:

  • 非常被低估的答案恕我直言。这通常很难捕捉到。
  • 这是人人需要的真理!!!转到您的模块并在声明部分添加这个特定的类
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 2021-05-28
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
相关资源
最近更新 更多