Vant3 ImagePreview 是一个基于 Vue3 的图片预览组件,它可以用于在网页上预览图片,支持手势缩放、滑动切换等功能。

使用方法如下:

  1. 安装 Vant3:
npm install vant@next -S
  1. 在 Vue 项目中引入 Vant3:
import { createApp } from 'vue';
import App from './App.vue';
import Vant from 'vant';
import 'vant/lib/index.css';

const app = createApp(App);
app.use(Vant);
app.mount('#app');
  1. 在需要使用的组件中引入 ImagePreview:
import { ref } from 'vue';
import { ImagePreview } from 'vant';

export default {
  setup() {
    const images = ref([
      'https://img.yzcdn.cn/vant/apple-1.jpg',
      'https://img.yzcdn.cn/vant/apple-2.jpg',
      'https://img.yzcdn.cn/vant/apple-3.jpg',
    ]);

    const showPreview = (index) => {
      ImagePreview({
        images: images.value,
        startPosition: index,
      });
    };

    return {
      images,
      showPreview,
    };
  },
};
  1. 在模板中使用 ImagePreview:
<template>
  <div>
    <img v-for="(item, index) in images" :key="index" :src="item" @click="showPreview(index)" />
  </div>
</template>

在点击图片时,调用 showPreview 方法,传入当前图片的索引,即可打开图片预览。

Vant3 图片预览组件 ImagePreview 使用教程