element-ui上传图片组件

做项目要上传图片,用到的地方比较多,就封装了组件,用了element-ui的upload,并且根据limit来限制上传的张数,超出部分隐藏,还有父子组件传值,传给后台的样式是数组形式。话不多数,贴上代码,加油各位!!

组件代码

HTML代码如下:

<template>
  <div class="about">
    <el-upload
      :class="{hide:hideUpload}"
      action="后台上传地址"
      list-type="picture-card"
      name="image"
      :data="{type:type}"
      :limit="limit"
      :before-remove="beforeRemove"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :on-change="handChange"
      :headers="headers"
      :file-list="pics"
    >
      <i class="el-icon-plus"></i>
      <div v-if="hideLimit" class="el-upload__tip" slot="tip">
        <span>可批量上传,最多上传{{limit}}</span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

js代码如下:

 <script>
export default {
  props: ["limit", "type", "pic"],
  data() {
    return {
      hideUpload: false,
      token: store.state.token,
      dialogImageUrl: "",
      dialogVisible: false,
      imgUrl: [],
      arr: [],
      headers: { Authorization: "Bearer " + localStorage.getItem("token")},
      hideLimit: true,
      pics: []
    };
  },
  mounted() {
    let _this = this;
    setTimeout(function() {
      if (_this.pic) {
        _this.pics = _this.pic;
        _this.hideUpload = _this.pic.length >= _this.limit;
        _this.getArr(_this.pic);
      }
      if (_this.type == "room" || _this.type == "house") {
        _this.hideLimit = false;
      }
    }, 500);
  },
  methods: {
    beforeRemove(file, fileList){
      console.log(file, fileList)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
      Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
        if (index > -1) {
          this.splice(index, 1);
        }
      };
      this.arr.remove(file.id);
      this.hideUpload = fileList.length >= this.limit;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res) {
      if (this.pic) {
        this.imgUrl = this.pic;
      }
      this.imgUrl.push(res.id);
      this.$emit("imgUrlarr", this.imgUrl);
    },
    handChange(file, fileList) {
      this.hideUpload = fileList.length >= this.limit;
    },
    //把json转数组,只存储id,方便传给后台
    getArr(item) {
      console.log(item);
      let arr = [];
      if (item) {
        item.forEach(function(item) {
          arr.push(item.id);
        });
      }
      console.log(arr);
      this.arr = arr;
      this.$emit("imgUrlarr", this.arr);
    }
  }
};
</script>

css如下:

<style lang="scss">
.hide .el-upload--picture-card {
  display: none;
}
.img {
  width: 148px;
  height: 148px;
  display: flex;
  img {
    width: 100%;
    height: 100%;
    margin-right: 10px;
  }
}
</style>

引入文件的代码

应用组件

   <upload-img @imgUrlarr="getImg" :pic="lockingfrom.pics" :type="type" :limit="2"></upload-img>

js

<script>
import uploadImg from "@/components/uploadImg";
export default{
components: {uploadImg},
	data(){
	return{
	   type: "customer",
	   lockingfrom: {
	     pics: ""
	   },
	 }
	},
	methods:{
	    getImg(e) {
	      this.lockingfrom.pics = e;
	    },
	}
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值