TypeScript:无法读取datepipe.transform中未定义的属性"transform" [英] TypeScript: Cannot read property 'transform' of undefined in datepipe.transform

查看:66
本文介绍了TypeScript:无法读取datepipe.transform中未定义的属性"transform"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此问题?
当我得到kekeh/ngx-mydatepicker日期并转换为字符串时,我有一个打字稿方法.

How can I resolve this problem?
I have a typescript method when I get the kekeh/ngx-mydatepicker date and transform to string.

这是我的课程:

import { DatePipe } from '@angular/common'

export class DateUtils {

public static getMyDatePickerDateToString(myDate: any): string {

    let datepipe: DatePipe;
    var date = new Date(myDate.date.year, myDate.date.month, myDate.date.day);

    return datepipe.transform(date, 'yyyy-MM-dd')
  }
}

这是我的错误:

ERROR
TypeError: Cannot read property 'transform' of undefined

推荐答案

您需要创建一个新的 DatePipe 对象.您声明 datepipe getMyDatePickerDateToString 方法的第一行,但实际上并未实例化.我不熟悉 DatePipe API,但可能类似于

You need to create a new DatePipe object. The first line of your getMyDatePickerDateToString method you declare datepipe but you don't actually instantiate one. I'm not familiar with the DatePipe API but it is probably something like

const datepipe: DatePipe = new DatePipe();

这篇关于TypeScript:无法读取datepipe.transform中未定义的属性"transform"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆