【问题标题】:error TS2322: Type '() => string' is not assignable to type 'string'错误 TS2322:类型 '() => string' 不可分配给类型 'string'
【发布时间】:2019-03-01 19:51:49
【问题描述】:

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

export class DateComponent implements OnInit {

  currentDate: string = new Date().toDateString;

  constructor() { }

  ngOnInit() {
  }
}

我收到以下错误,但我不知道是什么原因造成的。

错误 TS2322:类型 '() => string' 不可分配给类型 'string'。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您应该调用函数,而不是分配函数对象。在末尾添加() 应该可以解决它

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

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2019-03-19
      • 2022-07-22
      • 1970-01-01
      • 2021-09-26
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多