【发布时间】:2018-09-05 05:37:18
【问题描述】:
我有一个从类/模型 (projInfo) 获取数据的组件,包括一个日期对象。 我需要使用格式化的日期(日/月/年)的不同部分,以便我需要将它们分解并将它们放入一个数组中。
我似乎无法将 Date 对象解析为 string[] 类型。这是我所拥有的:
public _startDate = this.projInfo.startdato; //.toString();
@Input()
set startDate(startDate: string) {
// remove commas then split into array
const d: string = this.startDateFormat.replace(',', '');
this._startDate = d.split(' ');
}
最后一行的“this._startDate”提供错误Type 'string[]' is not assignable to type 'string'。
我该如何解决这个问题?很难找到答案,因为我认为错误太广泛了。
【问题讨论】:
-
您希望
_startDate是字符串还是字符串数组? -
什么是 startDateFormat ?
-
_startDate 是一个字符串,因为 this.projInfo.startDato 是一个字符串。 split 返回一个数组,因此您不能将 _startDate 与数组相等(实际上您可以定义 public _startDate:string|string[]=this.projInfo.startdato,但我认为您不想这样做)
标签: angular typescript casting