微信小程序之方法调用报错(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
                });
            }
        },
        // 其他代码不变
    });
}
相关推荐
龙兵科技小付说3 小时前
不同价格的上门做饭小程序APP都有什么功能?
小程序·软件开发·上门做饭
2501_916007475 小时前
如何查看 iOS 设备系统与硬件信息,iOS系统信息显示工具
android·ios·小程序·https·uni-app·iphone·webview
逆龙泰氽5 小时前
微信小程序开发04-1(小程序API)
微信小程序·小程序
说私域5 小时前
AI智能名片S2B2C商城小程序在微商中的应用与影响
大数据·人工智能·小程序·流量运营
苏苏哇哈哈6 小时前
微信小程序实现高性能动态配置水滴凹槽、凸起Tabbar 组件
微信小程序·小程序
逆龙泰氽7 小时前
微信小程序开发03(WXML语法)
微信小程序·小程序
2501_916007477 小时前
iOS APP 开发,从项目创建、证书与描述文件配置、安装测试和IPA 上传
android·ios·小程序·https·uni-app·iphone·webview
CHU7290357 小时前
淘宝扭蛋机小程序前端功能详解:以交互设计赋能趣味体验
java·前端·小程序·php
CHU7290351 天前
安心陪伴,便捷就医:陪诊代办小程序的温暖设计
前端·小程序·php
CHU7290351 天前
线上扭蛋机拆盒小程序前端功能版块解析
前端·小程序·php