错误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();