微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
AI行业应用研究4 小时前
破解活动统筹难题:会务小程序为活动组织提供全流程解决方案
小程序
万岳科技系统开发10 小时前
直播电商APP搭建如何支持多门店与多主播模式
小程序·架构
戈伊16 小时前
独立开发纪实:我如何用 Gemini CLI 和 Claude Code 打造一个“100% 含 AI 量”的小程序
微信小程序·ai编程
游戏开发爱好者817 小时前
iOS应用性能监控:Pre-Main与Main函数耗时分析及Time Profiler使用教程
android·ios·小程序·https·uni-app·iphone·webview
StarChainTech19 小时前
先享后付,正在悄悄改变电商的“信任游戏”
大数据·人工智能·游戏·微信小程序·小程序·软件需求
小羊Yveesss20 小时前
门店小程序外卖配送搭建实战:配送对接、运费策略与多门店调度方案
小程序·apache
tianxiaxue120 小时前
企业微信与小程序互联互通
小程序·企业微信
微擎应用1 天前
全渠道批发订货商城小程序管理系统
大数据·小程序
杰建云1671 天前
多商家入驻小程序平台怎么做
人工智能·小程序
Giggle12182 天前
开发上门维修(家政)小程序系统的核心功能和便捷方案
大数据·小程序