在我的应用程序中,我需要全局自定义管道,我尝试按照以下方式实现它角管但我总是看到这个错误
模板解析错误:找不到管道“formatdate”
格式化日期.管道
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'formatdate'
})
export class FormatdatePipe implements PipeTransform {
transform(dateJson: any, args?: any): any {
.
//code...
.
return dateJson;
}
}
}
应用程序模块
import { FormatdatePipe } from './shared/pipes/formatdate.pipe';
@NgModule({
declarations: [
AppComponent, FormatdatePipe
],
如果我将其导入到所有模块而不是主体 app.module 中,则该管道可以工作,我是否需要例程管道模块或其他东西
管道(如组件和指令)不像服务那样在全局范围内工作。
您需要在某个模块中定义管道。然后您可以在该模块中定义的组件中使用它。另一种方法是将管道添加到模块的导出中,然后将该模块导入到要使用它的模块中。
像这样定义它:
import { FormatdatePipe } from './shared/pipes/formatdate.pipe';
@NgModule({
declarations: [
FormatdatePipe
],
exports: [
FormatdatePipe
]
})
export class SomeUtilModule {}
然后将此模块导入到您想要使用它的地方,它应该可以工作:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)