element-ui upload组件多选文件上传改造
element ui 上传图片组件选择多图时报错 element-ui upload Cannot set property ‘status‘ of null
注意fileList是只读的,不能修改。我们这里使用uploadList来保存我们需要改动的数组,否则报错Cannot set property ‘status’ of null
问题分析:
每次上传都会促发 handleBeforeUpload和handleBeforeSuccess,传几张就会触发几次,经过反复调试,应该是handleBeforeSuccess上传回调方法执行多次$emit引起的
解决方案:
声明一个number变量用于记录上传图片数量,上传成功回调后通过判断图片数量与number一致时,执行一次$emit。
使用uploadList数组用来临时保存上传的多图数组
增加两个变量:
uploadList: [],
number:0
修改两个方法:
- handleBeforeUpload
handleBeforeUpload(file) {
...
this.number++;
},
- handleBeforeSuccess
handleUploadSuccess(res) {
//上传成功回调
this.uploadList.push({ name: res.fileName, url: res.data })
if(this.number==this.uploadList.length){
this.fileList = this.fileList.concat(this.uploadList)
this.uploadList = []
this.number = 0
this.$emit('input', this.listToString(this.fileList))
}
},