Vue Vant文件上传进阶版

Vue Vant文件上传进阶版

按照当前图片数保存数据,可以任意修改,当访问有数据时,实时显示

<template>
  <div class="imglist">
    <div class="h2">文件上传</div>
    <div class="upload">
      <van-uploader
        v-model="fileList"
        multiple
        capture="camera"
        :after-read="afterRead"
        @delete="deleturl"
      >
        <div class="imgsrc">
          <span class="icons icon-zhaoxiangji" />
        </div>
      </van-uploader>
    </div>
  </div>
</template>

<script>
//上传文件路径
import { uploadFile } from "@/api/upload";
export default {
  name: "uploaderimg",
  data() {
    return {
    //文件数组
    imgurl:"",
      value: "",
      fileList: []
    };
  },
  methods: {
  //加载
    loadData() {
      if (this.question.result) {     
      //图片保存的数据是按照1.jpg,2.png格式保存的
        let imgs = this.question.result.split(",");
        for (let index = 0; index < imgs.length; index++) {
        this.fileList.push({ url: "", image: "" });
          this.fileList[index].url = this.viewImgs(imgs[index]);
          this.fileList[index].image = imgs[index];
        }
      }
    },
    //读取文件完毕后的事件
    afterRead(file) {
         uploadFile(file.file).then(({ data }) => {
        this.fileList[this.fileList.length - 1] = { url: "" };
        //图片显示
        this.fileList[this.fileList.length - 1].url = this.viewImgs(data);
        //图片名
        this.fileList[this.fileList.length - 1].image = data;
        this.updateData();
      });
    },
    //文件删除之后的事件
    deleturl() {
      this.updateData();
    },
    updateData() {
      const list = [];
      for (let index = 0; index < this.fileList.length; index++) {
        list[index] = this.fileList[index].image ;
      }
      //把数组用,隔开变为字符串
      this.imgurl= list.join();
      //输出
      window.console.log(this.imgurl)
    }
  },
  mounted() {
    this.loadData();
  },
  computed: {
  //文件格式
    viewImgs() {
      return function(img) {
        return "../../../public/images/"+ img ;
      };
    }
  }
};
</script>

<style lang="scss" scoped>
.imglist {
  width: calc(100% - 50px);
  padding: 25px;
  background: white;
}
.h2 {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(113, 113, 113, 1);
  padding-bottom: 15px;
  text-align: left;
}
.imgsrc {
  margin-right: 10px;
  background: #eeeeee;
  width: 79px;
  display: flex;
  height: 57px;
  align-items: center;
  justify-content: center;
  align-content: center;
  border-radius: 3px;
}
.icon-zhaoxiangji {
  color: #cfcfcf !important;
  display: inline-block;
  font-size: 32px !important;
}
.upload {
  background: white;
  text-align: left;
}
/deep/.van-image {
  width: 79px;
  height: 57px;
}
/deep/.van-popup__close-icon--bottom-left {
  position: fixed;
}
/deep/.van-uploader__preview {
  margin: 0 10px 0px 0;
}
</style>

了解更多Vant组件的知识

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值