函数不是函数?
Function is not a function?
我有一个小问题,因为我想在 bootstrap-datepicker 的选项中使用我的函数。我有检查日期是否在日期数组中的功能:
isInArray(array, value) {
return !!array.find(item => {
return item.getTime() == value.getTime()
});
}
我想在此选项中使用它 (https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#beforeshowmonth),所以我将其放入我的选项中:
this.datepickerOptionsForMonths = {
minViewMode: 1,
format: "mm-yyyy",
startDate: this.dateFrom,
endDate: this.dateTo,
beforeShowMonth: function (date: Date) {
return this.isInArray(this.datepickerRange, date.getDate());
}
};
现在是问题,因为编译已完成,一切似乎都很好,但在控制台中出现错误:
this.isInArray is not a function
也许问题是我已经在 datepickerOptionsForMonths 所在的同一个主体中使用了这个函数(在 ngOnInit 中)。有人可以帮助我吗?
您正在更改 beforeShowMonth
函数的范围。
尝试改用箭头函数
beforeShowMonth: (date: Date) =>
this.isInArray(this.datepickerRange, date.getDate())
箭头函数维护封闭对象的范围。您可以阅读更多关于它的内容 here
我有一个小问题,因为我想在 bootstrap-datepicker 的选项中使用我的函数。我有检查日期是否在日期数组中的功能:
isInArray(array, value) {
return !!array.find(item => {
return item.getTime() == value.getTime()
});
}
我想在此选项中使用它 (https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#beforeshowmonth),所以我将其放入我的选项中:
this.datepickerOptionsForMonths = {
minViewMode: 1,
format: "mm-yyyy",
startDate: this.dateFrom,
endDate: this.dateTo,
beforeShowMonth: function (date: Date) {
return this.isInArray(this.datepickerRange, date.getDate());
}
};
现在是问题,因为编译已完成,一切似乎都很好,但在控制台中出现错误:
this.isInArray is not a function
也许问题是我已经在 datepickerOptionsForMonths 所在的同一个主体中使用了这个函数(在 ngOnInit 中)。有人可以帮助我吗?
您正在更改 beforeShowMonth
函数的范围。
尝试改用箭头函数
beforeShowMonth: (date: Date) =>
this.isInArray(this.datepickerRange, date.getDate())
箭头函数维护封闭对象的范围。您可以阅读更多关于它的内容 here