laravel - 如何在 VueJS 的 SetTimeOut 中提交存储
问题描述
我正在使用 Vue.js 在 Laravel 中制作应用程序。我想在触发方法时等待两秒钟,然后执行存储操作。但是,当我实现它时,我收到一个错误。
这是我的代码:
.listen('TeamLeaving', e => {
setTimeout(function() {
axios.get('/api/team/' + e.team.id + '/pulse').then(response => {
if (response.data === 0) {
// here is where it messes up
this.$store.commit('team/REMOVE_TEAM', e.team)
}
})
}, 2000)
// this.$store.commit('team/REMOVE_TEAM', e.team);
})
但是我收到一个错误:
Uncaught (in promise) TypeError: Cannot read property 'commit' of undefined
当我在它之外进行提交时,setTimeout
它工作得很好。所以我假设setTimeout
. 有人可以帮我解决这个问题吗?
解决方案
这篇文章可能会对您有所帮助:如何在 vueJs 方法中设置超时
重要的一点:
this
in 匿名函数附加到该匿名函数而不是您的主函数
你可以试试这个:
.listen('TeamLeaving', (e) => {
let vm = this;
setTimeout(function () {
axios.get('/api/team/'+ e.team.id + '/pulse')
.then(response => {
if (response.data === 0) {
//here is where it messes up
vm.$store.commit('team/REMOVE_TEAM', e.team)
}
});
}, 2000);
// this.$store.commit('team/REMOVE_TEAM', e.team);
});
推荐阅读
- sql-server - 将 Spark Dataframe 写入 SQL Server 时如何指定列数据类型
- tensorflow - 既然 TensorflowJS 可以通过 WebGL 使用 GPU,我为什么需要 nVIDIA GPU?
- java - 通常将数组分配给临时数组与创建新数组然后分配它有什么区别?
- spring-boot - 春季批处理 - 处理器或作家?
- r - 在 R 中使用循环和 Quantmod 收集股票信息
- java - 如何将字符串中的双打添加到双打数组列表中?
- python - 我的模型是过拟合还是欠拟合?.如何从这个损失曲线中识别它?
- python - Django - 编写测试以上传多个图像
- android - 在paytm阶段环境中付款后android webview转到https://developer.paytm.com/txn-response
- excel - 引用数据透视表中的一系列列