在新增時,需要給后端一個數組對象,對象里包含name和url屬性
在編輯時,后端會給前端返回一個數組對象,對象里包含name和url屬性
在使用el-upload組件上傳或者刪除文件時,組件會自動給:file-list綁定的數組對象中的所有對象,添加一個uid屬性,我們可以通過這個屬性,對數據進行新增或者刪除
<el-form-item label="附件">
<el-upload
class="attachment-uploader"
:action="uploadAttachmentUrl"
:on-remove="handleRemoveAttachment"
:before-remove="beforeRemoveAttachment"
:on-success="onAttachmentUploadSuccess"
multiple
:file-list="dataForm.attachmentList">
<el-button size="small" type="primary">點擊上傳</el-button>
</el-upload>
</el-form-item>
onAttachmentUploadSuccess (response, file, fileList) {
console.log('upload success')
this.dataForm.attachmentList.push({name: file.name, url: response.path})
console.log(this.dataForm.attachmentList)
},
// 刪除附件提示
beforeRemoveAttachment (file, fileList) {
return this.$confirm(`確定移除 ${file.name}?`)
},
// 刪除附件
handleRemoveAttachment (file, fileList) {
this.dataForm.attachmentList = this.dataForm.attachmentList.filter(item => item.uid !== file.uid)
},