報錯原因:設置了:file-list="fileList",filelist為只讀,修改后就會報錯 Cannot set property 'status' of undefined
解決方法:在成功回調中用setTimeout清空上傳列表即可解決。
代碼如下
<el-upload style="margin:10px 0 0 0;width:90%" class="upload-demo" ref="upload" :on-success="success" :action='url"' :data="icon" :auto-upload="false" accept=".jpg, .png, .jpeg" :headers="headers" :on-change="onchangeupload" :on-remove="onremove" :file-list="fileList">
<el-button slot="trigger" size="small" type="primary">{{$t('message.SelectFile')}}</el-button>
</el-upload>
//成功回調
uploadsuccess (res) {
if (res.code == "200") {
this.$message({
showClose: true,
message: res.message,
type: "success"
})
//-----------------------------------
//解決關鍵代碼:使用延時器清空上傳文件列表和自定義參數,
setTimeout(()=>{
this.uploadfile=false
this.icon={}
this.$refs.upload.clearFiles()
},100)
//------------------------------------
} else {
this.$message({
showClose: true,
message: res.message,
type: "error"
})
}
},
//失敗回調
uploaderror (res) {
this.$message({
showClose: true,
message: res.message,
type: "error"
})
},
//打開彈框
showUploadLogShow(val){
this.uploadLog=true
this.isUpload=val.isUpload
this.fileTypeobj={
fileType:"licenseFile",
fileId:val.fileId,
id:val.id
}
},
//刪除文件回調
onremove(val,list){
if(list.length==0){
this.uploadfile=false
}else{
this.uploadfile=true
}
},
onchangeupload(val,list){
this.fileList = list.slice(-1);//只上傳一個文件,自動更換最新選擇文件
if(list.length==0){
this.uploadfile=false
}else{
this.uploadfile=true
}
},
//觸發上傳
trueload() {
if(this.uploadfile){
if(this.isUpload){
this.$confirm(this.$t('message.xukewenjaincunzai'),this.$t('message.Tips'), {
confirmButtonText:this.$t('message.Ok'),
cancelButtonText: this.$t('message.Cancel'),
type: 'warning'
}).then(() => {
this.$refs.upload.submit()
this.uploadLog = false
this.getData()
}).catch(()=>{
this.$message({
showClose: true,
message:this.$t('message.Cancelupload'),
})
this.uploadLog = false
})
}else{
this.$refs.upload.submit()
this.uploadLog = false
this.getData()
}
}else{
this.$message({
showClose: true,
message:this.$t('message.Pleaseselectuploadfile'),
type: "error"
})
}
},