问题:
for循环无法解决数据获取的时候,在上传的图片,imageList虽然现实有数据,但是传到后端的时候,没有数据。
原因:同步和异步的问题。
解决:
1.在uploadFile.js中去掉
*@param - successc:成功回调
*@param - failc:失败回调
两个回调函数。
2.在function (filePath, dir) {函数中加入return,将oss地址return回去。
3.在publishMoment.vue中
async publish() {方法中,使用await this.getImageList();函数,
新增getImageList()函数:
其中为了加速oss上传速度,借助了这个插件:
地址:https://ext.dcloud.net.cn/plugin?id=278
插件ID:yushijie-ossutil
插件包体积:6.1KB
更新日期:2019-04-03版本:1.0.0
https://github.com/xieyushi/uniapp-alioss-util
getImageList(){
for (let i in this.imageTempList) {
let imageTemp = this.imageTempList[i];
let url=uploadImage(imageTemp.path, 'images/');
this.imageList.push({
url,
width: imageTemp.width,
height: imageTemp.height
});
}
这样就可以让异步请求变成同步请求,for循环结束后才会传递到后端。