gpt4 book ai didi

javascript - 未捕获的类型错误 : "(function call) is not a function"

转载 作者:行者123 更新时间:2023-12-02 21:01:22 25 4
gpt4 key购买 nike

我试图通过方法调用函数,但收到一条错误,指出它“不是函数”。我曾经在 HTML 中包含 JS 代码,但在将其移动到当前位置(使用类和构造函数)后,该功能停止工作。因此,我认为我缺少一些与类/构造函数相关的东西,但我不确定是什么。

index.js:

import calendarComponent from "./SiteAssets/scripts/calendar";

let isAdmin;

async function initComponents() {
isAdmin = await isAdminMember();
const { globalData, mmUser } = await globalInitProm(isAdmin);

const calendar = new calendarComponent(globalData, mmUser);
calendar.initRoutes();

document.getElementById('getFile').addEventListener('change', calendar.handleFileSelect, false); // ----- click event ----- //

}

initComponents();
<小时/>

calendar.js:

export default class {
constructor(globalData, mmUser) {
this.globalData = globalData;
this.isAdmin = mmUser.IsAdmin;
this.calendar = null;
}

initRoutes() {}

handleFileSelect(evt) {
console.log("handleFileSelect fired"); // works
let files = evt.target.files;

if (files.length > 0) {
this.parseAndUploadFile(files[0]); // "Uncaught TypeError: this.parseAndUploadFile is not a function"
}
}

parseAndUploadFile(file) {
console.log("trying to load excel file");
let reader = new FileReader();

reader.onload = function (e) {
// etc
};
}
}

最佳答案

要解决此问题,请使用绑定(bind)或使用委托(delegate)回调

export default class {
constructor(globalData, mmUser) {
this.globalData = globalData;
this.isAdmin = mmUser.IsAdmin;
this.calendar = null;
this.handleFileSelect = this.handleFileSelect.bind(this) // bind this here
}
// rest od the code

}

关于javascript - 未捕获的类型错误 : "(function call) is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61345660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com