出现错误: TypeError: Cannot read property 'RegisterStatues' of undefined,实际无法调用其他方法和变量,如:在handleRegister函数中无法调用RegisterStatues函数。
原因: 因为在success回调函数中,this指向的不再是Vue实例,而是success函数本身。因此,您需要在success函数外部保留正确的上下文,可以使用变量that 或者ES6的箭头函数 来解决这个问题。
修改后:
过在success函数中使用that来调用RegisterStatues函数
javascript
RegisterStatues: function() {
var that = this; // 保存正确的上下文
// 其余代码不变
},
handleRegister: function(e) {
const that = this; // 保存正确的上下文
// 其余代码不变
wx.request({
// 取消成功要更新报名状态
success: function(res) {
if (res.statusCode === 200) {
wx.showToast({
title: '取消报名成功',
icon: 'success',
duration: 2000
});
// 使用that来调用RegisterStatues函数
that.RegisterStatues();
} else {
wx.showToast({
title: '没找到报名信息',
icon: 'success',
duration: 2000
});
}
},
// 其他代码不变
});
}