功能不是功能?

D.Z*_*Zet 1 javascript typescript bootstrap-datepicker angular

我有一点问题因为我想在bootstrap-datepicker的选项中使用我的函数.我有功能检查日期是否在日期数组中:

isInArray(array, value) {
    return !!array.find(item => {
        return item.getTime() == value.getTime()
    });
}
Run Code Online (Sandbox Code Playgroud)

我想在这个选项中使用它(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());
    }
};
Run Code Online (Sandbox Code Playgroud)

现在是问题,因为编译完成,一切似乎都很好,但然后在控制台我遇到错误:

this.isInArray is not a function
Run Code Online (Sandbox Code Playgroud)

也许问题是我已经在datepickerOptionsForMonths所在的同一个体中使用过这个函数(在ngOnInit中).有人能帮我吗?

Bar*_*uch 6

您正在更改beforeShowMonth功能的范围.

请尝试使用箭头功能

beforeShowMonth: (date: Date) => 
  this.isInArray(this.datepickerRange, date.getDate())
Run Code Online (Sandbox Code Playgroud)

箭头函数维护封闭对象的范围.你可以在这里阅读更多内容