uniapp使用回调方法后this失效;uniapp如何自定义回调函数

本文探讨了如何在JavaScript的回调函数中正确地将调用方的`this`对象作为参数,以便在回调中访问和操作实例属性。通过实例代码展示了在检查码和扫码验证过程中如何使用这种方式来维护状态和调用外部方法。
摘要由CSDN通过智能技术生成

调用方,要将调用方的this当作参数传进 回调方法中,在回调中 参数 即this

eg:

,checkCodeFun(code,self,callBack){
				var that = this;
				var code = code;
				checkCode(that.$store.state.user.openId,{code: code,checkTypeValue:that.radioValue}).then((res) => {
					let resp = res.data;
					this.checkCodeRes.code = resp.code;
					this.checkCodeRes.msg = resp.msg;
					this.checkCodeRes.data.goodsName = resp.data.goodsName;
					this.checkCodeRes.data.nowProcessedNum = resp.data.nowProcessedNum;
					this.checkCodeRes.data.nowCompleteNum = resp.data.nowCompleteNum;
					this.checkCodeRes.data.defectiveNum = resp.data.defectiveNum;
					this.checkCodeRes.data.processName = resp.data.processName;
					this.checkCodeRes.data.processKey = resp.data.processKey;
					this.checkCodeRes.data.produceJobId = resp.data.produceJobId;
					callBack(self,resp);
					this.working(true);
				}).catch((res) => {
					uni.hideLoading();
					console.log(8882)
					console.error(res)
					this.working(true);
				})
			}
var that = this;
that.checkCodeFun(code,that,function(self,data){
					let that =self;
					if(code.length>=8){//2位标识-5位序号-n位id
						that.working(false);
						if(data.code != 0){
							that.addLog(1,data.msg);
							return;
						}
						if(that.radioValue == 1){
							uni.showLoading({
								title:'提交中...'
							})
							scancodesubmit(that.$store.state.user.openId,{code: code}).then((res) => {
								uni.hideLoading();
								console.log(777)
								console.log(res)
								console.log(res.data)
								let resp = res.data;
								if(resp.code==0){
									console.log(777.3)
									that.addLog(1,resp.msg);
								}else{
									that.addLog(1,resp.msg);
								}
								that.working(true);
							}).catch((res) => {
								uni.hideLoading();
								console.log(8882)
								console.error(res)
								that.working(true);
							})
						}
					}else{
						that.addLog(1,'非法条码:'+code);
					}
				})
				

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值