遇到的Vue elementUI图像上传不显示的问题

编者:李国帅

qq:9611153 微信lgs9611153

时间:2021-7-26

问题描述:

在使用Vue elementUI图像上传组件el-upload的时候,发现如果ruleForm没有原始数据,上传图片后,图片无法显示。

于是经过仔细的分析比较,发现只要把<el-form-item的绑定属性和<el-upload内部<img的属性设置为不同变量,上传完成后设置img的src即可。

解决方案:

  <el-dialog

        title="添加设备"

        :visible.sync="innerVisible"

        destroy-on-close

        append-to-body

      >

   <el-form

          :model="ruleForm"

          :rules="rules"

          ref="ruleForm"

          label-width="100px"

          style="width: 450px"

        >

         <el-form-item label="图片:" prop="imgUrl">

            <el-upload

              :data="otherData"

              list-type="picture"

              class="avatar-uploader"

              :action="uploadurl"

              :show-file-list="false"

              :on-success="handleAvatarSuccess"

            >

       <!-- <img v-if="ruleForm.imgUrl" :src="ruleForm.imgUrl" class="avatar">不可以 -->

              <img v-if="imgName" :src="imgNameclass="avatar" />

              <i v-else class="el-icon-plus avatar-uploader-icon"></i>

            </el-upload>

          </el-form-item>

        </el-form>

      </el-dialog>

 

参考:

https://element.eleme.cn/#/zh-CN/component/upload

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
可以使用 ElementUI 的 Upload 组件来实现图片上传显示。具体步骤如下: 1. 在 Vue 组件中引入 Upload 组件: ``` <template> <div> <el-upload class="upload-demo" action="/your-upload-api" :on-success="handleSuccess" :before-upload="beforeUpload" :file-list="fileList" list-type="picture-card"> <i class="el-icon-plus"></i> </el-upload> </div> </template> <script> export default { data() { return { fileList: [] }; }, methods: { handleSuccess(response, file, fileList) { // 上传成功后的回调函数 console.log(response); }, beforeUpload(file) { // 上传前的校验函数 const isJPG = file.type === 'image/jpeg'; const isPNG = file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG && !isPNG) { this.$message.error('上传图片只能是 JPG/PNG 格式!'); return false; } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!'); return false; } return true; } } }; </script> ``` 2. 在 handleSuccess 回调函数中,将上传成功后的图片地址保存到 fileList 中: ``` handleSuccess(response, file, fileList) { // 上传成功后的回调函数 console.log(response); this.fileList.push({ name: file.name, url: response.data.url }); } ``` 3. 在模板中使用 v-for 循环遍历 fileList,将图片显示出来: ``` <template> <div> <el-upload class="upload-demo" action="/your-upload-api" :on-success="handleSuccess" :before-upload="beforeUpload" :file-list="fileList" list-type="picture-card"> <i class="el-icon-plus"></i> </el-upload> <div v-for="(item, index) in fileList" :key="index"> <img :src="item.url" alt=""> </div> </div> </template> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值