微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
笨笨狗吞噬者3 小时前
uni-app 运行时揭秘:styleIsolation 的转化
前端·微信小程序·uni-app
double_eggm6 小时前
微信小程序3
微信小程序·小程序
杰建云1676 小时前
小程序如何提升留存?
小程序·小程序制作
PinTrust SSL证书6 小时前
Geotrust企业型OV通配符SSL
网络协议·网络安全·小程序·https·云计算·ssl
怀君7 小时前
Uniapp——微信小程序Canvas层级过高问题解决
微信小程序·小程序·uni-app
杰建云1679 小时前
小程序如何提升转化率?
小程序·小程序制作
杰建云1679 小时前
小程序如何做用户运营?
小程序·产品运营·用户运营
Devil枫9 小时前
【腾讯位置服务开发者征文大赛】AI 赋能小程序地图开发:腾讯地图 Miniprogram Skill 实战记录
人工智能·小程序
00后程序员张1 天前
完整教程:如何将iOS应用程序提交到App Store审核和上架
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张1 天前
iOS应用性能优化全解析:卡顿、耗电、启动与瘦身
android·ios·性能优化·小程序·uni-app·iphone·webview