vue基于element-ui的el-upload實(shí)現(xiàn)上傳和回顯(回顯取消動(dòng)畫解決詭異的問(wèn)題)

在圖片回顯的時(shí)候會(huì)出現(xiàn)這樣的問(wèn)題,如圖:

image.png

這種情況,要解決的話,只需要加入兩行css。記住要使用樣式穿透,否則不生效。

::v-deep .el-list-leave-active {
  transition: none;
}
::v-deep .el-list-leave-to {
  transition: none;
}
image.png

這樣就不會(huì)出現(xiàn)第一張圖的問(wèn)題了,但是當(dāng)限制一張圖片的時(shí)候,上傳按鈕消失了,但是還在占位,只需要加一個(gè)自定義class類名,并加個(gè)樣式就行了。如下:

<el-upload
    :file-list="fileList"
    action=""
    list-type="picture"
    :on-remove="handleRemove"
    :limit="1"
    :on-change="handleEditChange"
    :http-request="uploadImgMainImg"
    :class="{ disUoloadSty:fileList.length>0 }"
    accept="image/png, image/jpeg, image/jpg"
    style="width:45%;">
    <el-button v-if="fileList.length < 1"
         size="mini"
         style="width: 130px"
         type="danger">點(diǎn)擊上傳</el-button>
</el-upload>

沒(méi)錯(cuò),就是 :class="{ disUoloadSty:fileList.length>0 }"這個(gè),樣式如下:
style="width:45%;" width 過(guò)小,會(huì)自動(dòng)隱藏 fileListname

.disUoloadSty {
   ::v-deep .el-upload{
    display:none;   /* 上傳按鈕隱藏 */
    }
   ::v-deep .el-upload-list__item:first-child{
     margin-top: 0;
   }
}

最后就是這樣。


image.png

下面是上傳組件對(duì)應(yīng)的幾個(gè)方法,如下:

handleRemove(file, fileList) {
      this.fileList = [];
    },
uploadImgMainImg(content) {
      const file = content.file;
      var formData = new FormData();
      formData.append("image", file);
      upload(formData).then((res) => {
        this.formData.avatar = res.data.avatar;
        this.$message.success("上傳成功!");
      });
    },
handleEditChange(file, fileList) {
      this.fileList = fileList;
    },
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容