uni-app request回调函数内无法使用this.

tech2025-11-14  5

微信小程序开发中,通常会在 request成功的回调函数中修改本地的属性。如果直接使用this,会有类似的提示无法修改: > Cannot set property 'xxx' of undefined;at api request success callback function T 而且因为我用uni开发微信小程序和h5 刚开始一直在号h5端调试 不报错 但是this.phonenumber也没数据 后来启动微信开发工具调试的时候才报错

找到一位博主写的解决方案 ,在此谢过 博主解决方案地址 我的代码如下

/* uni.setStorageSync("phoneNumber",this.phoneNumber); console.log(uni.getStorageSync("phoneNumber"),this.phoneNumber) return; */ var _this = this; uni.request({ url:this.preUrl+'App.User.GetCheckCode', method:"POST", header: { 'content-type': 'application/x-www-form-urlencoded' }, data:{ phoneNumber : this.phoneNumber }, success(res) { console.log(res); var data = res.data; if(data.ret == 200){ if(data.data.res == 'OK'){ uni.setStorageSync("checkCode",data.data.code); uni.setStorageSync("username",_this.phoneNumber); console.log(uni.getStorageSync("checkCode"),uni.getStorageSync("username")); //验证码发送成功后 获取按钮置灰 开始倒计时 60秒后可重新获取 _this.showText = true; _this.second = 60; var interval = setInterval(() => { --_this.second }, 1000) setTimeout(() => { clearInterval(interval) _this.showText = false }, 60000) console.log(_this.showText); }else{ console.log('发送验证码失败',res.data.res) } }else{ console.log('服务器异常') } }, fail(error) { console.log(error) } }) //把里面的值传回去 this.showText = _this.showText
最新回复(0)