最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue開發(fā)之封裝上傳文件組件與用法示例

 更新時(shí)間:2019年04月25日 11:32:02   作者:guanguan0_0  
這篇文章主要介紹了Vue開發(fā)之封裝上傳文件組件與用法,結(jié)合實(shí)例形式分析了vue.js使用elementui的 el-upload插件進(jìn)行上傳文件組件的封裝及使用相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Vue開發(fā)之封裝上傳文件組件與用法。分享給大家供大家參考,具體如下:

使用elementui的 el-upload插件實(shí)現(xiàn)圖片上傳組件

每個(gè)項(xiàng)目存在一定的特殊性,所以數(shù)據(jù)的處理會(huì)不同

pictureupload.vue:

<template>
  <div class="pictureupload">
    <el-upload
        :action="baseUrl + '/api/public/image'"
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
        :file-list="fileList"
        :on-exceed="handleExceed"
        :before-remove="beforeRemove"
        name="img"
        :on-success="upLoadSuccess"
        :data="upLoadData"
        :headers="headers"
        :limit="limit">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import store from "@/store";
export default {
  props: {
    imgList: {
      type: Array,
      default: []
    }, // 父組件傳遞的圖片列表
    limit: "" // 圖片數(shù)量限制
  },
  data() {
    return {
      fileList: [],
      upLoadData: {
        img: ""
      },
      baseUrl: process.env.BASE_API,
      dialogVisible: false,
      dialogImageUrl: "",
      headers: {
        Authorization: store.getters.token_type + " " + store.getters.token
      } // 接口調(diào)用token
    };
  },
  watch: {
    // 監(jiān)聽imgList的變化: fileList要求的格式為[{url: img}],所以監(jiān)聽imgList變化后將其進(jìn)行處理,賦值給fileList
    imgList: {
      handler(newValue, oldValue) {
        if(newValue.length === 0) {
          this.fileList = [];
          return;
        }
        for (let i = 0; i < newValue.length; i++) {
          if (oldValue[i] != newValue[i]) {
            this.fileList = [];
            newValue.forEach(el => {
              this.fileList.push({url: el})
            })
            return;
          }
        }
      },
      deep: true
    }
  },
  methods: {
    // 圖片移除時(shí)處理數(shù)據(jù)
    handleRemove(file, fileList) {
      let item = [];
      fileList.forEach(el => {
        item.push(el.url);
      });
      this.$emit("removeimg", item);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 判斷圖片數(shù)量
    handleExceed(files, fileList) {
      this.$message.warning(`當(dāng)前限制選擇 ${this.limit} 個(gè)文件,本次選擇了 ${files.length} 個(gè)文件,共選擇了 ${files.length + fileList.length} 個(gè)文件`);
    },
    beforeRemove(file, fileList) {},
    // 上傳圖片成功事件
    upLoadSuccess(response) {
      this.$emit("uploadimg", response.message);
      this.$message("上傳成功",);
    }
  },
  mounted() {
    if (this.imgList.length != 0) {
      this.imgList.forEach(el => {
        this.fileList.push({ url: el });
      });
    }
  }
};
</script>

使用上傳圖片組件:

<pictureupload @uploadimg="uploadimg" :imgList="ruleForm.img" :limit="3" @removeimg="removeimg"></pictureupload>

removeimg(item) {
  this.ruleForm.img = item;
},
uploadimg(item) {
  this.ruleForm.img.push(item);
},

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Vue項(xiàng)目引進(jìn)ElementUI組件的方法

    Vue項(xiàng)目引進(jìn)ElementUI組件的方法

    這篇文章主要介紹了Vue項(xiàng)目引進(jìn)ElementUI組件的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-11-11
  • web前端Vue報(bào)錯(cuò):Uncaught?(in?promise)?TypeError:Cannot?read?properties?of?nu解決

    web前端Vue報(bào)錯(cuò):Uncaught?(in?promise)?TypeError:Cannot?read?

    這篇文章主要給大家介紹了關(guān)于web前端Vue報(bào)錯(cuò):Uncaught?(in?promise)?TypeError:Cannot?read?properties?of?nu的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • vue3?中v-model語(yǔ)法糖示例詳解

    vue3?中v-model語(yǔ)法糖示例詳解

    vue3中的v-model相當(dāng)于vue2中的v-model和v-bind.sync 修飾符組合在一起的產(chǎn)物(擇優(yōu)整合)v-bind.sync 在 vue3 中被移除了可以在組件標(biāo)簽上使用多個(gè) v-model 綁定屬性,使用參數(shù)區(qū)分,這篇文章主要介紹了vue3?中v-model語(yǔ)法糖,需要的朋友可以參考下
    2024-06-06
  • 在vue中使用G2圖表的示例代碼

    在vue中使用G2圖表的示例代碼

    這篇文章主要介紹了在vue中使用G2圖表的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • vue實(shí)現(xiàn)購(gòu)物車列表

    vue實(shí)現(xiàn)購(gòu)物車列表

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • vue中如何下載excel流文件及設(shè)置下載文件名

    vue中如何下載excel流文件及設(shè)置下載文件名

    這篇文章主要介紹了vue中如何下載excel流文件及設(shè)置下載文件名,對(duì)vue感興趣的同學(xué),可以參考下
    2021-05-05
  • Vue動(dòng)態(tài)引用json數(shù)據(jù)的兩種方式

    Vue動(dòng)態(tài)引用json數(shù)據(jù)的兩種方式

    在 Vue 項(xiàng)目中引用 JSON 文件非常簡(jiǎn)單,尤其是當(dāng)文件內(nèi)容是一個(gè)數(shù)組時(shí),本文給大家介紹了Vue動(dòng)態(tài)引用json數(shù)據(jù)的兩種方式,并有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2025-04-04
  • vue使用vue-cli快速創(chuàng)建工程

    vue使用vue-cli快速創(chuàng)建工程

    這篇文章主要介紹了vue使用vue-cli快速創(chuàng)建工程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Vue proxyTable配置多個(gè)接口地址,解決跨域的問(wèn)題

    Vue proxyTable配置多個(gè)接口地址,解決跨域的問(wèn)題

    這篇文章主要介紹了Vue proxyTable配置多個(gè)接口地址,解決跨域的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼

    vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07

最新評(píng)論

安乡县| 娄烦县| 根河市| 莱西市| 缙云县| 南华县| 乌审旗| 肇东市| 吉林市| 晋宁县| 微山县| 淮滨县| 长武县| 宝兴县| 瑞金市| 吴忠市| 华容县| 景洪市| 新营市| 嵊泗县| 孟连| 柳州市| 祁东县| 宜黄县| 阜城县| 富蕴县| 东至县| 额尔古纳市| 九龙县| 夏津县| 来凤县| 康马县| 图木舒克市| 天镇县| 襄汾县| 乌鲁木齐市| 霸州市| 嵊州市| 宣武区| 荥经县| 龙南县|