element el-upload新增或編輯文章時上傳與刪除文件


在新增時,需要給后端一個數組對象,對象里包含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)
      },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM