先举个例子
let a=0;
page({
wx.uploadfile({
cloudPath ,//假设有值
filePath ,//假设有值
success:res=>{
a=1;
console.log(a);//在这里a=1;
}
})
consloe.log(a);//在这里a=0;相当于赋值操作无效
})
源代码展示
uploadCloudImage:function(){
let that=this;
let userInfo=app.globalData.userInfo;
let username=userInfo.nickName;
let Time="",uploadImage=[];//uploadImage临时变量
let img_url=that.data.img_url,filePath,cloudPath;//img_url便是要赋值的图片数组
for(let i=0;i<img_url.length;i++){
Time=utils.formatTime(new Date());
filePath = img_url[i];
cloudPath = username+`广场my-image`+`/`+Time+`${filePath.match(/\.[^.]+?$/)[0]}`;
wx.cloud.uploadFile({
cloudPath ,
filePath ,
success: res => {
console.log('[上传文件] 成功:', res);
img_url[i]=res.fileID;//赋值
that.setData({
img_url:img_url,//赋值给页面数据
})
console.log(img_url);
uploadImage.push(res.fileID);
},
fail: e => {
console.error('[上传文件] 失败:', e)
wx.showToast({
icon: 'none',
title: '上传失败',
})
},
complete: () => {
wx.hideLoading();
}
})
}
console.log(that.data.img_url);
},
必须是0 了解一下 promise async,await
试试在 success 上面 let that = this ;