我正在尝试在我的 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]数组
现在将此模块导入到您希望它工作的模块中。
关于 Angular 4 模板 "Pipe could not be found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44916392/