欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件

 更新時(shí)間:2021年09月15日 08:33:02   作者:qq_25993655  
這篇文章主要為大家詳細(xì)介紹了Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件,供大家參考,具體內(nèi)容如下

html部分

<template>
  <div class="contWrap">
    <van-uploader
      v-model="fileList"
      :multiple="true"
      :before-read="beforeRead"
      :after-read="afterRead"
      :before-delete="delUploadImg"
      upload-icon="plus"
    >
    <!-- 注:這里是自定義上傳樣式 -->
      <!-- <p>
        <van-icon
          name="plus"
          color="#07c160"
          size=".5rem"
        />
        上傳照片
      </p> -->
    </van-uploader>
  </div>
</template>

js部分

<script>
import axios from "axios";
export default {
  name: "uploadImages",
  data() {
    return {
      fileList: [],
      uploadUrl: "/api/upload/fileUpload",
      headers: {
        token: this.$store.state.account.token,
      },
    };
  },

  methods: {
    // 返回布爾值
    beforeRead(file) {
      if (file instanceof Array) {
        //判斷是否是數(shù)組
        file.forEach((v) => {
          this.checkFile(v);
        });
      } else {
        this.checkFile(file);
      }
      return true;
    },
    //圖片類型檢查
    checkFile(file) {
      const format = ["jpg", "png", "jpeg"];
      const index = file.name.lastIndexOf(".");
      const finalName = file.name.substr(index + 1);
      if (!format.includes(finalName.toLowerCase())) {
        Toast("請(qǐng)上傳" + format.join(",") + "格式圖片");
        return false;
      }
    },
    afterRead(file) {
    // 此時(shí)可以自行將文件上傳至服務(wù)器
      if (file instanceof Array) {
        file.map((v) => {
          v.status = "uploading";
          v.message = "上傳中...";
          this.uploadImg(v);
        });
      } else {
        file.status = "uploading";
        file.message = "上傳中...";
        this.uploadImg(file);
      }
    },
    //上傳
    uploadImg(file) {
      const formData = new FormData();
      formData.append("file", file.file);
      axios
        .post(this.uploadUrl, formData, {
          headers: this.headers,
        })
        .then((res) => {
          if (res.data) {
            if (file instanceof Array) {
              //判斷是否是數(shù)組
              file.map((v, i) => {
                v.status = "success";
                v.message = "";
                v.url = res.data[i];
              });
            } else {
              file.status = "success";
              file.message = "";
              file.url = res.data.uploadUrl;
            }
          }
        })
        .catch((err) => {
          this.$notify({
            type: "warning",
            message: "上傳失敗",
          });
        });
    },
    //刪除
    delUploadImg(item) {
      this.fileList = this.fileList.filter((v) => v.url != item.url);
    }
  },
};
</script> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue的虛擬DOM使用方式

    vue的虛擬DOM使用方式

    這篇文章主要介紹了vue的虛擬DOM使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Vue3結(jié)合TypeScript項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)記錄

    Vue3結(jié)合TypeScript項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)記錄

    聽(tīng)說(shuō)有的公司已經(jīng)開(kāi)始用vue3了 趕緊打開(kāi)B站學(xué)一下,下面這篇文章主要給大家介紹了關(guān)于Vue3結(jié)合TypeScript項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • vue cli4.0 如何配置環(huán)境變量

    vue cli4.0 如何配置環(huán)境變量

    這篇文章主要介紹了vue cli4.0 如何配置環(huán)境變量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳解Vue CLI3配置之filenameHashing使用和源碼設(shè)計(jì)使用和源碼設(shè)計(jì)

    詳解Vue CLI3配置之filenameHashing使用和源碼設(shè)計(jì)使用和源碼設(shè)計(jì)

    這篇文章主要介紹了詳解Vue CLI3配置之filenameHashing使用和源碼設(shè)計(jì)使用和源碼設(shè)計(jì),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 最新評(píng)論