微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
头发还在的女程序员12 小时前
基于JAVA语言的短剧小程序-抖音短剧小程序
java·开发语言·小程序
2501_9160074717 小时前
iOS 应用性能测试的工程化流程,构建从指标采集到问题归因的多工具协同测试体系
android·ios·小程序·https·uni-app·iphone·webview
源码_V_saaskw17 小时前
JAVA国际版同城跑腿源码快递代取帮买帮送同城服务源码支持Android+IOS+H5
android·java·ios·微信小程序
tbit18 小时前
fluwx 拉起小程序WXLog:Error:fail to load Keychain status:-25300, keyData null:1
flutter·ios·微信小程序
book多得18 小时前
刷题专用微信小程序推荐
微信小程序·小程序
00后程序员张20 小时前
iOS 抓不到包怎么办?从 HTTPS 解密、QUIC 排查到 TCP 数据流分析的完整解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
技术与健康21 小时前
微信小程序云开发实践:共享环境与LLM整合经验
微信小程序·小程序
老华带你飞1 天前
社区养老保障|智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·社区养老保障
发财北1 天前
本地生活小程序开发方案
小程序
游戏开发爱好者82 天前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone