微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
禾高网络18 小时前
开启环保新潮流:回收小程序系统功能介绍
java·大数据·小程序
laboratory agent开发21 小时前
多维度拆解小程序平台:数字化经营工具的理性选择指南
微信小程序
2501_9159214321 小时前
详细解析,iOS 应用上架 App Store 的完整流程与指南
android·ios·小程序·https·uni-app·iphone·webview
微三云 - 廖会灵 (私域系统开发)21 小时前
小程序商城系统安全运维剖析:漏洞、风控与长效迭代方案
运维·小程序·系统安全
2501_9159184121 小时前
Xcode调试iOS应用内存管理与优化,识别泄漏与性能监控指南
android·ios·小程序·uni-app·cocoa·iphone·xcode
T01156181 天前
独立开发者商单实战|商超门店小程序(自提 + 同城配送):小程序全端页面 & 业务流程总图汇总
微信小程序·小程序·个人开发·前端实战
T01156181 天前
独立开发者商单实战|商超门店小程序(自提 + 同城配送)用户端 & 后台联动踩坑上篇
小程序
梦想的颜色1 天前
UniApp 四端对齐实战:iOS App / Android App / 小程序 / H5 兼容坑点与完整解决方案|AI 辅助多端落地
android·ios·小程序·uni‑app·uniapp 四端适配·多端兼容·ai 生成 uni‑app
黄华SJ520it2 天前
二二复制定点裂变双轨商城系统开发:原理、架构与实战指南
运维·小程序·架构·系统开发
驳是2 天前
入坑 UniApp 写小程序,一些感受
微信小程序