ImagePreview 图片预览
图片预览组件:原生 API 薄壳——三小程序端与 H5 统一走 uni.previewImage (缩放与滑动由原生预览页免费提供),组件本身不渲染遮罩,只渲染 default slot 作为触发器包裹;open(index) / close() 为命令式开关,API 面与 plus 自绘 形态保持同构。服务商品详情看大图、相册浏览等场景。
平台差异(登记):本端为 uni.previewImage 薄壳——plus 的预览是自绘 Viewer 弹层(teleport 到 body + 手势缩放 / 滑动翻页 / 滚轮缩放),小程序没有 document 与 teleport 的等价物,本仓不再自绘弹层,改调
uni.previewImage: H5 由 uni 运行时实现浮层、微信端走原生预览页面。组件只渲染触发器包裹, 下标换算(loop 归一 / 钳制)、开关语义与无障碍标签全部复用 share core 纯函数。 无原生关闭 API:close()只置modelValue = -1并抛close,原生预览页 的关闭动作平台不回调,受控态需使用方在合适时机主动收口。
基础用法
组件包裹触发器内容(default slot),ref 调 open() 拉起原生预览:
<template>
<lh-image-preview
ref="previewRef"
:images="images"
v-model="current"
>
<button @click="previewRef?.open()">预览</button>
</lh-image-preview>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const previewRef = ref<{ open: (index?: number) => void; close: () => void } | null>(null)
const current = ref(-1)
const images = [
'https://example.com/1.png',
'https://example.com/2.png',
'https://example.com/3.png',
]
</script>open(index) 默认打开第 0 张;下标先经 resolveImagePreviewIndex 归一(loop 取模回绕 / 非循环钳制到边界),拉起成功才回写 modelValue 并抛 change。
指定起始下标与受控开关
v-model 绑定当前下标(-1 = 关闭);外部把 modelValue 写成有效下标会被 视为「打开」(拉起原生预览),写成 -1 视为「关闭」:
<lh-image-preview
ref="previewRef"
:images="images"
v-model="current"
/>// 打开第 3 张(下标 2)
previewRef.value?.open(2)
// 等价的受控写法
current.value = 2
// 关闭:置 -1 并抛 close(无原生关闭 API,登记差异)
previewRef.value?.close()列表场景
缩略图列表点击预览对应图,是商品详情 / 相册的典型用法:
<view
v-for="(src, i) in images"
:key="i"
@click="previewRef?.open(i)"
>
<image :src="src" mode="aspectFill" />
</view>Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| images | 图片地址列表(空值自动过滤) | string[] | — |
| modelValue | 当前下标(受控;-1 或不传 = 关闭) | number | -1 |
| loop | 循环翻页(false 时首尾钳制) | boolean | true |
| showIndex | 展示下标指示(原生预览承载,登记不生效) | boolean | true |
| swipeDuration | 滑动动画时长 ms(登记不生效) | number | 300 |
| closeOnPressOverlay | 点击遮罩关闭(登记不生效) | boolean | true |
| showClose | 桌面端展示关闭按钮(登记不生效) | boolean | true |
| minZoom | 双指缩放下限(原生预览消费,登记不生效) | number | 1 |
| maxZoom | 双指缩放上限(原生预览消费,登记不生效) | number | 3 |
| label | 无障碍可达名称(默认「图片预览 第 i / N 张」) | string | — |
| customClass | 自定义类名(透传到根节点) | string | — |
| sx | sx 快速布局属性 | LhSxProps | — |
方法(defineExpose)
| 方法名 | 说明 | 参数 |
|---|---|---|
| open | 打开预览:下标经 resolveImagePreviewIndex 归一后调 uni.previewImage,成功回写 modelValue 并抛 change | index?: number(默认 0) |
| close | 关闭预览:无原生关闭 API,置 modelValue = -1 并抛 close | — |
事件
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| update:modelValue | 下标回写(v-model) | value: number |
| change | 下标变化(open / 受控切换时触发) | index: number |
| close | 关闭(close 或外部置 -1 时触发) | — |
作用域插槽
default slot 作为触发器包裹,作用域参数:
| 参数 | 说明 | 类型 |
|---|---|---|
| index | 当前下标(-1 = 关闭) | number |
| total | 图片总数 | number |
| indexText | 下标文案(如 1 / 5,空列表为空串) | string |
| open | 打开方法(透传,供触发器直接调用) | (index?: number) => void |
| close | 关闭方法 | () => void |
无障碍
根节点携带 role="dialog" + aria-modal="true",aria-label 默认取 share core 的「图片预览 第 i / N 张」,可用 label 自定义。
平台差异
- 降级为
uni.previewImage原生薄壳(全端可用):缩放与滑动由原生预览页 提供,本端零手势实现;没有遮罩透明度 / 关闭按钮 / 翻页按钮 / 下标指示 / 滑动时长的自定义能力,对应 prop 为 API 面占位(登记不生效)。 - 无原生关闭 API:
close()只置modelValue = -1并抛close;原生预览页 的关闭(点击空白 / 返回键)平台不回调,受控态由使用方主动收口。 - 拉起失败兜底:
open()裹 try/catch,极端环境(如 H5 未注入 uni 对象) 抛错时记 warn 并跳过——不回写modelValue、不抛change。 - 空列表守卫:
images为空(或全部为空值)时open()静默跳过, 不调原生 API、不抛事件。 - 样式产物:share 样式源的
lh-image-preview规则面向 web 自绘形态 (遮罩 / 翻页按钮等),薄壳端无对应节点,本组件不消费 style.css 产物。
与 Vue3 版的差异
- 逻辑口径(下标归一、可见判定、无障碍标签)与 plus 完全一致(同一 share core)。
- 视图由自绘 Viewer 弹层改为
uni.previewImage原生 API(见「平台差异」)。 - 手势(双指缩放 / 滑动翻页 / 滚轮缩放)全部由原生能力承接,本端不实现。
- 增加了
customClassprop:uni-app 端自定义类名无法穿透 styleIsolation,统一走 prop 透传。

