element ui 上传图片

简介: 笔记

20.png

  • 这玩意很简单,记录一下吧,给入门的小白用下


1.template 部分


<template>
  <div class="editPage__img">
    <div class="title">图片设置</div>
    <div class="img__con">
      <el-upload
        class="avatar-uploader"
        :action="uploadImgUrl"
        :data="uploadImgData"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <p><span>(点击图片即可替换其它图片)</span><br>说明:图片宽度为750PX,格式为JPG或者PNG,每张图片大小不超过3M</p>
    </div>
  </div>
</template>


2. script 部分


data() {
    return {
      imageUrl: this.rightData.imageUrl,
      // 图片上传
      uploadImgUrl: `${process.env.VUE_APP_BASE_API}/common-server/aliFile/upLoadFileNoSecret`,
      uploadImgData: { busiName: 32 },
      // 应付多个组件的情况 记录当前组件的key值
      componentKey: null,
    };
  },
  methods: {
    uploadImg() {},
    handleAvatarSuccess(res, file) {
      // console.log(res)
      this.imageUrl = res.data.url;
       this.$emit("childRightFn", {
        ...this.rightData,
        ...{ imageUrl: this.imageUrl}, 
        ...{props: {src: this.imageUrl}} 
      });
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png";
        // 限制只能3M以内的图片
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error("图片只能是 JPG 或者 PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isJPG && isLt2M;
    },
  },


3. scss 部分


  • 请大家按需取用,不需要删掉就是,不要原封不动的搬
<style lang="scss" scoped>
.editPage__img {
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 16px;
    color: #8c939d;
    width: 350px;
    height: 30px;
    line-height: 30px;
    text-align: center;
  }
  .avatar {
    width: 350px;
    height: auto;
    display: block;
  }
  .title {
    font-size: 18px;
    margin-bottom: 20px;
  }
  .img__con {
    p {
      margin-top: 10px;
      text-align: center;
      span{
        color: #409eff;
      }
    }
    .el-button {
      width: 100%;
      margin: 10px 0 20px 0;
    }
  }
}
</style>
目录
相关文章
|
1月前
element ui实现多层级复杂表单的操作(添加与回显)之表单操作交互操作
element ui实现多层级复杂表单的操作(添加与回显)之表单操作交互操作
33 0
|
1月前
Element UI之el-tabs的样式修改字体颜色、下划线、选中/未选中
Element UI之el-tabs的样式修改字体颜色、下划线、选中/未选中
154 0
|
17天前
|
JavaScript
Vue给Element UI的el-popconfirm绑定按钮事件
Vue给Element UI的el-popconfirm绑定按钮事件
|
1月前
有关element UI el-table 跟el-dialog搭配使用出现的问题,背景问题,穿透问题
有关element UI el-table 跟el-dialog搭配使用出现的问题,背景问题,穿透问题
42 0
|
2天前
|
JavaScript
vue element ui 打开弹窗出现黑框问题
vue element ui 打开弹窗出现黑框问题
15 1
|
14天前
【UI】 element ui 表格没有数据时用--填充
【UI】 element ui 表格没有数据时用--填充
20 2
|
1月前
|
前端开发
element ui实现多层级复杂表单的操作(添加与回显)之回显功能实现
element ui实现多层级复杂表单的操作(添加与回显)之回显功能实现
14 0
|
1月前
|
JSON 数据格式
element ui实现多层级复杂表单的操作(添加与回显)之添加功能实现
element ui实现多层级复杂表单的操作(添加与回显)之添加功能实现
13 0
|
1月前
|
JavaScript 数据库
vue使用element ui实现下拉列表分页的功能!!!
vue使用element ui实现下拉列表分页的功能!!!
32 0
|
1月前
|
Web App开发 前端开发 JavaScript
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
27 1

热门文章

最新文章