Skip to content

Image 图片

用于展示网络图片,内置加载中占位、失败占位和点击预览能力,适合列表封面、头像区块、卡片头图等场景。

交互式调试

🔧调试模式Playground

基础用法

vue
<lk-image src="https://example.com/demo.jpg" width="200rpx" height="200rpx" />

尺寸与圆角

vue
<template>
  <lk-image src="https://picsum.photos/200/200" width="160rpx" height="160rpx" />
  <lk-image src="https://picsum.photos/200/200" width="160rpx" height="160rpx" radius="24rpx" />
  <lk-image src="https://picsum.photos/200/200" width="160rpx" height="160rpx" radius="50%" />
</template>

填充模式

fit 直接映射到底层图片的展示模式。

vue
<template>
  <lk-image src="https://picsum.photos/400/300" width="200rpx" height="160rpx" fit="cover" />
  <lk-image src="https://picsum.photos/400/300" width="200rpx" height="160rpx" fit="contain" />
  <lk-image src="https://picsum.photos/400/300" width="200rpx" height="160rpx" fit="fill" />
</template>

加载与错误占位

默认情况下会展示加载中和加载失败状态,可按需关闭。

vue
<template>
  <lk-image src="https://invalid.example.com/demo.jpg" show-loading show-error />
</template>

点击预览

开启 preview 后,点击图片会调起系统图片预览。

vue
<lk-image
  src="https://picsum.photos/800/600"
  width="240rpx"
  height="180rpx"
  radius="var(--lk-radius-md)"
  preview
/>

推荐示例

1) 直接复用项目 Demo(推荐)

vue
<script setup lang="ts">
import ImageDemo from '@/components/demos/image-demo.vue'
</script>

<template>
  <ImageDemo />
</template>

2) 在业务页中按需组合

vue
<template>
  <view class="page-demo">
    <lk-image />
  </view>
</template>

API

Props

参数说明类型默认值
src图片地址string''
width容器宽度string'100%'
height容器高度string'auto'
radius圆角string'var(--lk-radius-md)'
fit填充模式cover | contain | fill | none | scale-downcover
lazy是否懒加载booleanfalse
showLoading是否显示加载中占位booleantrue
showError是否显示失败占位booleantrue
preview是否开启点击预览booleantrue
id根节点 idstring''
customClass根节点自定义类名string | object | array
customStyle根节点自定义样式string | object

Events

事件名说明参数
load图片加载成功(event: unknown) => void
error图片加载失败(event: unknown) => void
click点击图片容器({ src, event }) => void
preview调起图片预览前触发({ src, urls, event }) => void
preview-success图片预览成功调起后触发({ src, result }) => void
preview-fail图片预览调起失败时触发({ src, error }) => void

Slots

使用建议

TIP

在列表场景中建议总是显式传入 widthheight,这样加载前后布局更稳定。

WARNING

开启 preview 后,点击事件仍会先触发 click,如果业务中要阻止预览,需要在组件外自行控制是否传入 preview

基于 MIT 协议开源 · 粤ICP备2022114240号-3

Image 图片

正在连接预览服务...

请先运行 pnpm run dev:h5