加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular使用自定义管道

发布时间:2020-12-17 17:59:07 所属栏目:安全 来源:网络整理
导读:我很乐意帮助将内置管道导入角度为2的自定义管道. 这是我的代码: @Pipe({ name: 'tablePipe' })export class TablePipe implements PipeTransform { constructor(private decimalPipe: DecimalPipe) { } transform(field: any,format: Format,formatArg: st
我很乐意帮助将内置管道导入角度为2的自定义管道.

这是我的代码:

@Pipe({ name: 'tablePipe' })

export class TablePipe implements PipeTransform {
    constructor(private decimalPipe: DecimalPipe) {

    }
    transform(field: any,format: Format,formatArg: string): any {
        let formattedField: any = ''
        switch (format) {
            case 'number':
                {
                    formattedField = this.decimalPipe.transform(field,formatArg);
                    break;
                }
        }
        return formattedField;
    }
}

export type Format = 'date' | 'string' | 'number';

这是我得到的错误:

EXCEPTION: Uncaught (in promise): Error: No provider for DecimalPipe!

在组件中导入常规自定义管道时,我使用:

@Component({
  ...,pipes: [MyCustomPipe],...
})

解决方法

为此,您需要将DecimalPipe添加到某处的提供程序

例如

@NgModule({
  providers: [DecimalPipe],...
})
export class AppModule {}

您还可以将其添加到组件的提供者(您使用管道的组件或祖先组件).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读