【问题标题】:Function is not a function? [duplicate]函数不是函数? [复制]
【发布时间】:2019-01-16 14:03:49
【问题描述】:

我有一个小问题,因为我想在 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 中)。有人可以帮我吗?

【问题讨论】:

  • 这条线return item.getTime() == value.getTime() 工作吗? item 只是一个可能没有 getTime 方法的变量?
  • 另请注意,您可以使用Array.prototype.some 而不是Array.prototype.find 以避免需要强制转换为布尔值。

标签: javascript angular typescript bootstrap-datepicker


【解决方案1】:

您正在更改 beforeShowMonth 函数的范围。

尝试使用箭头函数

beforeShowMonth: (date: Date) => 
  this.isInArray(this.datepickerRange, date.getDate())

箭头函数维护封闭对象的范围。你可以阅读更多关于它here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2019-06-14
    • 2017-03-22
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多