el-upload上传图片

<template>
  <el-container>
    <div :class="{'hideAddBtn':fileList.length}">
      <el-upload
        :action="uploadImage"
        :headers="{ token:jdb_token }"
        :limit="1"
        :file-list="fileList"
        :on-exceed="handleExceed"
        :on-success="handleSuccess"
        :on-remove="handleRemove"
        :before-upload="beforeUpload"
        accept=".jpg, .jpeg, .png, .gif, .bmp"
        list-type="picture-card"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
    </div>
  </el-container>
</template>

<script>
import searchArticle from "../../../api/searchArticle.js";
import { mapState } from "vuex";
export default {
  data() {
    return {
      fileList: [],
      uploadImage: process.env.VUE_APP_API + searchArticle.uploadImage
    };
  },
  computed: {
    ...mapState(["jdb_token"])
  },
  methods: {
    beforeUpload(file) {
      //上传图片前
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    handleSuccess(file, fileList) {
      //此处的fileList是个对象
      //上传成功
      if (file.code === 200) {
        this.fileList.push(fileList);
      }
    },
    handleExceed() {
      //图片超出上限
      this.$message.error("当前限制选择 1 个文件");
    },
    handleRemove(file, fileList) {
      //移除图片
      this.fileList = [];
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/ .hideAddBtn .el-upload--picture-card {
  display: none;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值