微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
凉辰14 小时前
uniapp实现生成海报功能 (开箱即用)
javascript·vue.js·小程序·uni-app
奶糖 肥晨14 小时前
微信小程序定位功能开发实战:实现自动定位、城市切换与地图导航
微信小程序·小程序
笨笨狗吞噬者15 小时前
【uniapp】小程序支持分包引用分包 node_modules 依赖产物打包到分包中
前端·微信小程序·uni-app
用户693717500138419 小时前
微信悄悄搞大事!原生智能助手秘密研发,2026年改变亿人使用习惯
android·后端·微信小程序
CHU72903519 小时前
一蔬一饭总关情:生鲜买菜商城APP的生活美学
小程序·生活
CHU72903520 小时前
扭蛋机盲盒小程序:趣味交互与惊喜体验的功能设计
前端·小程序
CHU72903520 小时前
AI辅助工具小程序:多元功能助力,开启智能便捷新体验
前端·人工智能·小程序
sheji341621 小时前
【开题答辩全过程】以 互助式失物招领微信小程序为例,包含答辩的问题和答案
微信小程序·小程序
一字白首21 小时前
进阶实战:微信小程序路由、交互与生命周期全攻略DAY03
微信小程序·小程序·交互