微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
code_li6 小时前
小程序上线需要的资质证书汇总
小程序·上线·发布·资质
hnxaoli14 小时前
统信小程序(十三)循环键鼠操作程序
python·小程序
i查拉图斯特拉如是15 小时前
使用workbuddy 30分钟搭建微信小程序
微信小程序·小程序
IceSugarJJ16 小时前
Open-AutoGLM项目学习
语言模型·微信小程序·github
2501_9160088917 小时前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
android·macos·ios·小程序·uni-app·iphone·webview
咖啡の猫19 小时前
小程序协同工作和发布
小程序
维双云20 小时前
小程序怎么制作工具?与其盲目找开发,不如先分清自己要哪一种
小程序
qq_91094629220 小时前
校园共享电动车租赁小程序
小程序
ZC跨境爬虫20 小时前
模块化烹饪小程序开发日记 Day6:(菜谱列表接口开发与日志调试实践)
前端·javascript·css·ui·微信小程序·html
侃谈科技圈20 小时前
5G网络仿真软件哪个更高效?Ranplan两款核心产品深度解析
小程序