vue 使用vant中的图片预览功能

Vant 的 ImagePreview 组件提供了便捷的图片预览功能,支持函数调用和组件注册两种方式。用户可以通过设置 `startPosition` 指定初始显示图片,通过 `closeable` 添加关闭按钮,并监听 `onClose` 事件处理关闭操作。此外,还支持异步关闭和自定义内容。在实际应用中,如文件预览场景,可以根据文件类型调用不同预览方式,如图片使用 ImagePreview,PDF 则跳转到相应预览页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ImagePreview 图片预览

介绍

图片放大预览,支持函数调用和组件调用两种方式。

函数调用

ImagePreview 是一个函数,调用函数后会直接在页面中展示图片预览界面。

import { ImagePreview } from 'vant';

ImagePreview(['https://img01.yzcdn.cn/vant/apple-1.jpg']);

组件调用

通过组件调用 ImagePreview 时,可以通过下面的方式进行注册。

import Vue from 'vue';
import { ImagePreview } from 'vant';

// 全局注册
Vue.use(ImagePreview);

// 局部注册
export default {
  components: {
    [ImagePreview.Component.name]: ImagePreview.Component,
  },
};

代码演示

基础用法

直接传入图片数组,即可展示图片预览。

ImagePreview([
  'https://img01.yzcdn.cn/vant/apple-1.jpg',
  'https://img01.yzcdn.cn/vant/apple-2.jpg',
]);

指定初始位置

ImagePreview 支持传入配置对象,并通过 startPosition 选项指定图片的初始位置(索引值)。

ImagePreview({
  images: [
    'https://img01.yzcdn.cn/vant/apple-1.jpg',
    'https://img01.yzcdn.cn/vant/apple-2.jpg',
  ],
  startPosition: 1,
});

展示关闭按钮

设置 closeable 属性后,会在弹出层的右上角显示关闭图标,并且可以通过 close-icon 属性自定义图标,使用close-icon-position 属性可以自定义图标位置。

ImagePreview({
  images: [
    'https://img01.yzcdn.cn/vant/apple-1.jpg',
    'https://img01.yzcdn.cn/vant/apple-2.jpg',
  ],
  closeable: true,
});

监听关闭事件

通过 onClose 选项监听图片预览的关闭事件。

import { Toast } from 'vant';

ImagePreview({
  images: [
    'https://img01.yzcdn.cn/vant/apple-1.jpg',
    'https://img01.yzcdn.cn/vant/apple-2.jpg',
  ],
  onClose() {
    Toast('关闭');
  },
});

异步关闭

通过 asyncClose 属性可以开启异步关闭,开启后异步关闭后,只能通过实例上的 close 方法关闭图片预览。

const instance = ImagePreview({
  images: [
    'https://img01.yzcdn.cn/vant/apple-1.jpg',
    'https://img01.yzcdn.cn/vant/apple-2.jpg',
  ],
  asyncClose: true,
});

setTimeout(() => {
  instance.close();
}, 2000);

组件调用

如果需要在图片预览内嵌入组件或其他自定义内容,可以使用组件调用的方式,调用前需要通过 Vue.use 注册组件。

<van-image-preview v-model="show" :images="images" @change="onChange">
  <template v-slot:index>第{{ index }}页</template>
</van-image-preview>
export default {
  data() {
    return {
      show: false,
      index: 0,
      images: [
        'https://img01.yzcdn.cn/vant/apple-1.jpg',
        'https://img01.yzcdn.cn/vant/apple-2.jpg',
      ],
    };
  },
  methods: {
    onChange(index) {
      this.index = index;
    },
  },
};

后面是我使用的真实案例,使用的是函数调用。

 

这是图片列表,点击进行预览。

//页面渲染
<div class="more_center">
     <ul>
        <li v-for="(item,index) in upload.fileList" :key="index" @click="readFile(item)">
            <div class="li_images">
                 <i class="iconfont icon-caozuoshouce" style="font-size:20px;"></i>
            </div>
            <div class="li_name">
                 <span>{{item.fileName}}</span>
            </div>
            <div class="li_enter">
                 <i class="iconfont icon-arrow-right" style="font-size:18px;"></i>
            </div>
        </li>
     </ul>
</div>



//js实现
<script>
import { ImagePreview } from 'vant';
import { listFileinfo, delFileinfo, upload } from '@/api/system/fileinfo'
export default {
    data(){
        return{
            baseURL: process.env.VUE_APP_BASE_API,
        }
    },
    methods:{
        readFile(file){//预览
            if(file.fileName.endsWith('png')  || file.fileName.endsWith('jpg') || file.fileName.endsWith('jpeg')){
                ImagePreview({images:[this.baseURL+file.filePath],closeable:true});
            }else if(file.fileName.endsWith('pdf')){
                this.$router.push({
                    name:"FilePreview",
                    params:{
                        file:file
                    }
                })
            }else{//下载
                window.location.href = this.baseURL + "/common/download/resource?resource=" + encodeURI(file.filePath);
            }
        },
        getList() {
            listFileinfo(this.queryParams).then(res => {
                this.upload.fileList = res.rows
            })
        },
    },
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

甜甜凉白开

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

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

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

打赏作者

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

抵扣说明:

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

余额充值