微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
2501_9151063211 分钟前
深入解析HTTPS抓包原理、中间人攻击及反抓包技术攻防
数据库·网络协议·ios·小程序·https·uni-app·iphone
silvia_Anne28 分钟前
微信小程序商品列表
微信小程序·小程序
游戏开发爱好者81 小时前
React Grab工具详解:AI助力Vue3、Svelte和Solid前端元素调试
android·ios·小程序·https·uni-app·iphone·webview
维双云1 小时前
做一个教培类做题类型的小程序多少钱?
小程序
小羊Yveesss1 小时前
2026年商家小程序外卖怎么找骑手?
小程序
ze^02 小时前
Day05 APP应用&微信小程序&原生态开发&H5+Vue技术&封装打包&反编译抓包点
vue.js·微信小程序·小程序
程序鉴定师19 小时前
深圳小程序公司推荐 助力企业数字化转型优质服务商
大数据·小程序
用户8574824354801 天前
useList 通用列表管理hook
vue.js·微信小程序
陪小甜甜赏月1 天前
微信小程序分享onShareAppMessage
前端·微信小程序·小程序
weikecms1 天前
本地生活 CPS 返利小程序搭建,支持外卖 + 出行 + 加油 + 酒店
小程序·生活