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

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

/* 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
经验分享 程序员 微信小程序 职场和发展