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>