当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Vue开发之封装上传文件组件与用法示例

Vue开发之封装上传文件组件与用法示例

2019年07月21日  | 移动技术网IT编程  | 我要评论

本文实例讲述了vue开发之封装上传文件组件与用法。分享给大家供大家参考,具体如下:

使用elementui的 el-upload插件实现图片上传组件

每个项目存在一定的特殊性,所以数据的处理会不同

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: "" // 图片数量限制
  },
  data() {
    return {
      filelist: [],
      uploaddata: {
        img: ""
      },
      baseurl: process.env.base_api,
      dialogvisible: false,
      dialogimageurl: "",
      headers: {
        authorization: store.getters.token_type + " " + store.getters.token
      } // 接口调用token
    };
  },
  watch: {
    // 监听imglist的变化: filelist要求的格式为[{url: img}],所以监听imglist变化后将其进行处理,赋值给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: {
    // 图片移除时处理数据
    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;
    },
    // 判断图片数量
    handleexceed(files, filelist) {
      this.$message.warning(`当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + filelist.length} 个文件`);
    },
    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);
},

希望本文所述对大家vue.js程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网