javascript - 我不能调用 this.function: TypeError: this.fetchData is not a function
问题描述
我打电话时收到此错误“TypeError:this.fetchData is not a function” this.fetchData();
,我认为它this
丢失了所以我尝试使用bind
或箭头函数但它也不起作用,如何解决这个问题?
class ShowResults extends Component {
getData() {
return setTimeout(function() {
this.fetchData();
}, 2000); //tried to use bind but did not work--> .bind(this);
}
}
async fetchData() {
//code
}
}
解决方案
正如您所说,您需要.bind(this)
因为您不在 setTimeout 函数内的同一范围内;您能否发送更多代码,您的括号数量有些奇怪。
推荐阅读
- vb.net - VS 无法开始调试,因为缺少调试目标
- javascript - 将html导出到docx jquery
- javascript - 应用程序未针对 iPad Air 2 更新,仍然显示错误消息
- python - 如何解决“连接到 SMTP 主机时出错:[Errno 10061] 由于目标机器主动拒绝,无法建立连接”?
- sql - 如何获取包含该列的值或子数组的所有记录?
- linux - 带有外壳扩展的系统单元配置
- python - 从 JSON 中删除 JSON 对象?
- lttng - Raspbian 上的 Linux 跟踪工具 lttng:内核跟踪器不可用
- sql - 在几行中增加唯一值
- maven - maven 发布插件失败