微信小程序之方法调用报错(TypeError: Cannot read property ‘RegisterStatues‘ of undefined)

出现错误: 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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
衍生星球1 小时前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
hnxaoli11 小时前
统信小程序(十六)xls转xlsx
开发语言·python·小程序
卓怡学长12 小时前
w272基于springboot便民医疗服务小程序
java·spring boot·spring·小程序·intellij-idea
新华财经频道12 小时前
2026微信小程序搭建平台实测测评,优缺点解析
微信小程序·小程序
kidding7231 天前
旋转大转盘小程序
前端·css·微信小程序·小程序·前端框架
码农客栈1 天前
小程序学习(二十九)之项目打包
小程序
凡科建站1 天前
2026年艺术类教育小程序开发平台有哪些?艺术类教育小程序开发平台推荐
小程序
LT10157974442 天前
2026年在线兼容性测试工具推荐|零部署网页 / APP / 小程序实测对比
测试工具·小程序
码农客栈2 天前
小程序学习(二十八)之“订单列表”
小程序
这是个栗子3 天前
uni-app 微信小程序开发:常用事件指令(@xxx)(一)
微信小程序·小程序·uni-app