类型'{的参数来自:字符串;到:字符串; }'不可分配给'string |类型的参数日期'。在flatpickr中启用日期

时间:2019-05-16 11:26:54

标签: angular typescript web flatpickr

我正在尝试在与angular集成的flatpickr日历中启用日期。因此,当我尝试推送启用日期对象以启用flatpickr的配置选项时,我得到了Argument of type '{ from: string; to: string; }' is not assignable to parameter of type 'string | Date'.

这是配置的代码。的flatpickr:-

this.exampleOptions = {
  dateFormat: "d.m.Y",
  minDate: "today",
  maxDate: "",
  onChange: function (selectedDates, dateStr) {
    // console.log('selected date is ', selectedDates);
    console.log('latest selected dates ', dateStr);
    this.selectedDate = selectedDates;
    this.dateSelected = selectedDates;
    console.log('this.dateSelected ', this.dateSelected);
    console.log('this.selectedDate ', this.selectedDates);
  }
};

在一个函数中,我正在使用该配置对象来启用日期:-

allAboutDateAndTime(){
  if (this.experienceData.dateRangeList != null && this.experienceData.dateRangeList != undefined && this.experienceData.dateRangeList.length > 0) {
  this.experienceData.dateRangeList.forEach((date: DateRange) => {
    const from = date.startDate.toString();
    const to = date.endDate.toString();
    const enable = {
      from: from,
      to: to
    };
    this.exampleOptions.enable.push(enable); // GETTING ERROR HERE
  });
}

这是我遇到的错误

Argument of type '{ from: string; to: string; }' is not assignable to parameter of type 'string | Date'. Type '{ from: string; to: string; }' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 38 more

我希望它能够启用Flatpickr documentaion

中指定的日期

2 个答案:

答案 0 :(得分:1)

当您在使用Angular时,我想您一定在使用ng2-flatpickr。然后我尝试下载他们的Javascript软件包flatpickrng2-flatpickr,我发现他们的angular软件包不具备JS软件包的所有功能!

根据其JS软件包,该 enable 属性应为下图所示的类型...

enter image description here

根据其Angular软件包,启用属性应为...类型。

enter image description here

因此,可能无法使用角度包来实现您所需的功能。您应该尝试在您的angular项目中使用JS包,或者必须在其Angular包github repo中提出问题。

Bug - enable option should support function #57

已经存在一个与此相关的问题,要求他们使 enable 属性应像JS软件包一样接受该功能!

因此您可以在此处提出问题!或者,如果您想在项目中以某种方式实现它,请尝试使用其JS软件包!

答案 1 :(得分:0)

您是否尝试过使用相同的console.log(this.experienceData)检查对象的类型以及对象的属性和方法?也许永远不会分配enable数组,而必须在使用空数组之前对其进行初始化?

似乎您正在尝试为现有属性分配其他类型的属性,或推送到不包含您要引入的此类对象的数组。

尝试一下:

this.exampleOptions.enable = [{             来自:“ 2025-04-01”,             到:“ 2025-05-01”         }]

如果仍然出错,则您的代码中存在某些错误,错误地分配了类型或没有首先初始化数组。

更新

尝试

this.exampleOptions = {
  dateFormat: "d.m.Y",
  minDate: "today",
  maxDate: "",
  enable: ["2015-12-01"]
  onChange: function (selectedDates, dateStr) {
    // console.log('selected date is ', selectedDates);
    console.log('latest selected dates ', dateStr);
    this.selectedDate = selectedDates;
    this.dateSelected = selectedDates;
    console.log('this.dateSelected ', this.dateSelected);
    console.log('this.selectedDate ', this.selectedDates);
  }
};

尝试初始化对象以及enable数组和一些适合该格式的随机值,然后查看它是否起作用。否则,请尝试为每个变量分配适当的类型。