Vue2.0+Vant图片预览(ImagePreview)无法显示照片

1.安装

通过 npm  安装vant2 的版本命令:cnpm i vant@latest-v2 -Snpm i vant@latest-v2 -S

2.应用代码

在全局(App.vue)中需要引入和注册全局组件:如下代码

import Vue from 'vue';

import Vant from 'vant';

import 'vant/lib/index.css';

Vue.use(Vant);

template:
        <!-- 剧照swiper组件 -->
      <detail-swiper :perview="2" name="photos">
        <detail-swiper-item v-for="(data,index) in detailObj.photos" :key="index">
          <div class="avatar" :style="{backgroundImage:'url('+data+')'}"                         
           @click="handlePreview(detailObj.photos,index)"></div>
        </detail-swiper-item>
      </detail-swiper>

<script>
//引入vue
import Vue from 'vue'
// 函数调用法
import { ImagePreview } from 'vant';
export default{
    return{
      detailObj:null,
    }
    methods:{
         handlePreview(photos,index){
            ImagePreview({
              images: photos, //照片数组
              startPosition: index, //照片开始位置
              closeable: true, // 关闭按钮
              closeIconPosition:'top-left'   //关闭按钮的位置
      });
    }

    }
    created(){
    // axios 通过id 可以请求后台数据
    http().then(res=>{
        this.detailObj = res.data.data.film
      })
  }
}
}
</script>

3.图片无法正常显示,其余功能正常;报错如下信息;

 

 4.原因分析

原因:vant安装版本问题;现在默认安装的是peerDependencies。

 

5.解决方案

通过下面命令重新安装vant(2),

npm i vant@latest-v2 --legacy-peer-deps;

重新安装后package.json中vant版本配置会变化如下

 6.重启服务器,重新刷新页面,问题解决

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我是一名AI语言模型,无法为您提供完整的Vue2 Vant附件图片上传公用组件。以下是一个简单的示例供您参考: ```vue <template> <div class="upload"> <!-- 选择文件 --> <van-uploader v-model="fileList" accept="image/*" multiple @before-read="beforeRead" > <van-icon slot="upload-icon" name="photo-o" /> </van-uploader> <!-- 预览选中的文件 --> <van-image-preview :images="previewImages" /> <!-- 显示已选择的文件 --> <van-cell-group> <van-cell v-for="(file, index) in fileList" :key="file.name" :title="file.name" :label="file.status === 'uploading' ? '上传中...' : ''" :value="file.url" :is-link="file.status === 'done'" @click="preview(index)" > <template v-if="file.status === 'done'"> <img :src="file.url" class="preview-image" /> </template> </van-cell> </van-cell-group> </div> </template> <script> import { Toast, Uploader, Icon, Cell, CellGroup, ImagePreview } from 'vant'; export default { components: { Uploader, Icon, Cell, CellGroup }, data() { return { fileList: [], previewImages: [], }; }, methods: { // 读取文件前进行压缩处理 async beforeRead(files) { for (const file of files) { if (/^image\//.test(file.type)) { try { const compressedFile = await this.compress(file); file.url = URL.createObjectURL(compressedFile); file.compress = compressedFile; } catch (e) { console.error(e); Toast.fail('图片压缩失败'); } } } }, // 压缩图片 async compress(file) { const maxWidth = 800; const maxHeight = 800; const quality = 0.8; const reader = new FileReader(); reader.readAsDataURL(file); return new Promise((resolve, reject) => { reader.onload = (event) => { const img = new Image(); img.src = event.target.result; img.onload = () => { const canvas = document.createElement('canvas'); let width = img.width; let height = img.height; if (width > height) { if (width > maxWidth) { height *= maxWidth / width; width = maxWidth; } } else { if (height > maxHeight) { width *= maxHeight / height; height = maxHeight; } } canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0, width, height); canvas.toBlob((blob) => { const compressedFile = new File([blob], file.name, { type: file.type }); resolve(compressedFile); }, file.type, quality); }; img.onerror = () => { reject(new Error('图片加载失败')); }; }; }); }, // 预览选中的文件 preview(index) { this.previewImages = this.fileList .filter((file) => /^image\//.test(file.type)) .map((file) => ({ url: file.url })); ImagePreview({ images: this.previewImages, startPosition: index }); }, }, }; </script> <style> .preview-image { width: 60px; height: 60px; object-fit: cover; } </style> ``` 该组件使用了Vant的Uploader组件进行文件上传,并在beforeRead事件中进行了图片压缩处理。同时,预览选中的文件使用了VantImagePreview组件。请注意,由于IOS设备拍摄的照片可能会包含EXIF信息,因此需要在压缩图片时使用toBlob方法并指定图片类型,以避免出现旋转问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值