Angular 4 模板 "Pipe could not be found"

标签 angular

我正在尝试在我的 Angular 4 项目中使用定制的管道。无法弄清楚为什么编译器无法在组件模板中识别我的管道。

QFormat管道

@Pipe({  name: 'qFormat'})
export class QFormatPipe implements PipeTransform {...}

共享模块

@NgModule({
    declarations: [QFormatPipe],
    exports: [QFormatPipe]
})
export class SharedModule { }

特征模块

@NgModule({
  imports: [SharedModule],
  declarations: [MyComponent]
})
export class FeatureModule{ }

MyComponent.html

<div>{{someProp | qformat}}</div>
                  ^^^^^^^

模板抛出错误:

Angular: the pipe 'qformat' could not be found

我错过了什么吗?

最佳答案

在我的例子中,问题是通过 CLI 生成是在 app.module.ts 中添加管道,但我正在使用的组件的模块是不同的。或者在您的情况下,它可能是在您正在使用的模块中未注册管道/错误注册管道

所以一个快速的解决方案是:

Delete the pipe you've created (Don't forget to remove it from app.module.ts). Then create a folder like pipes: Then generate a module for it like 'ng g module pipes/pipes-common'

然后生成类似“ng g pipe myPipeExample”的管道,现在不要忘记添加它

声明:[myPipeExamplePipe] 数组和

导出:[myPipeExamplePipe]数组

现在将此模块导入到您希望它工作的模块中。

This question has multiple very helpful answers

关于 Angular 4 模板 "Pipe could not be found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44916392/

相关文章:

Angular 11 没有运行 ngcc

javascript - angular 5, reactive form-重置所需的表单控件不会重置输入下所需的错误

Angular Material 7 使用带有拖放功能的网格

angular - 调用插值,即使 ChangeDetectionStrategy.OnPush

javascript - typescript 泛型 : Use output argument type to build input argument type

angular - 如何在 Angular Universal 中获取完整的基本 URL(包括服务器、端口和协议(protocol))?

javascript - Angular Material 2 - 如何将垫子工具栏和垫子标签锁定到顶部

javascript - Angular Material 垫步进器 : How to use the same formgroup for multiple steps?

javascript - 如何在 Angular 4 中将 observable<User> 转换为 Observable<boolean>

angular - 如何在 Ionic 2 中设置带有选项卡的侧面菜单?