错误TS2322:类型'()=>字符串"不可分配给“字符串"类型 [英] error TS2322: Type '() => string' is not assignable to type 'string'

查看:92
本文介绍了错误TS2322:类型'()=>字符串"不可分配给“字符串"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular 6的新手.使用以下代码:

I am new to Angular 6. With the following code:

export class DateComponent implements OnInit {

  currentDate: string = new Date().toDateString;

  constructor() { }

  ngOnInit() {
  }
}

我遇到以下错误,但我不知道是什么原因引起的.

I am getting the following error, but I do not know what may be causing it.

错误TS2322:类型'()=>字符串'不能分配给类型'字符串'.

error TS2322: Type '() => string' is not assignable to type 'string'.

推荐答案

您应调用函数,而不要分配函数对象.最后添加()应该可以解决

You should invoke the function, not assign the function object. Adding () at the end should fix it

currentDate: string = new Date().toDateString();

这篇关于错误TS2322:类型'()=>字符串"不可分配给“字符串"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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