Skip to content

Image 图片 ​

17:06

说明 ​

在 uni-app 原生 image 基础上做了增强:内置加载中 / 加载失败占位,可用 loading、error 插槽或 loading-icon / error-icon 替换;支持 fade 淡入、shape 形状与 border-radius 圆角、bg-color 占位底色、lazy-load 懒加载。width / height 传数值时按 px 处理,不传则宽 100%、高 auto。

兼容性 ​

平台支持情况
App✅ 支持
H5✅ 支持
微信小程序✅ 支持(webp、show-menu-by-longpress 仅微信小程序生效)

示例 ​

基础用法 ​

默认 square(小圆角 4px)展示,可通过 width / height 自定义宽高,形状设置见下方示例。

查看源码
vue
<template>
    <view class="row">
        <ux-image src="https://placehold.co/100x100.png" width="100" height="100" />
        <ux-image custom-class="gap" src="https://placehold.co/200x200.png" width="200" height="200" />
    </view>
</template>

<style scoped>
.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.gap {
    margin-left: 12px;
}
</style>

圆角与形状 ​

shape 控制形状:square(小圆角 4px,默认) / circle(正圆);border-radius 一经传入即优先于 shape,传 0 即为真直角。

查看源码
vue
<template>
    <!-- shape 两档全演示:square 小圆角(4px,不传即此档) / circle 正圆 -->
    <view class="row">
        <ux-image src="https://placehold.co/100x100.png" width="100" height="100" shape="square" />
        <ux-image custom-class="gap" src="https://placehold.co/100x100.png" width="100" height="100" shape="circle" />
    </view>
    <!-- border-radius 一经传入即优先于 shape 预设,传 0 即真直角 -->
    <view class="row">
        <ux-image src="https://placehold.co/100x100.png" width="100" height="100" :border-radius="10" />
        <ux-image custom-class="gap" src="https://placehold.co/100x100.png" width="100" height="100" :border-radius="0" />
    </view>
</template>

<style scoped>
.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.gap {
    margin-left: 12px;
}
</style>

加载失败状态 ​

加载失败时显示错误占位图标,可通过 error-icon 自定义。

查看源码
vue
<template>
    <ux-image src="https://invalid.example.com/not-found.png" width="100" height="100" />
</template>

裁剪模式 ​

mode 控制图片裁剪/缩放模式,对比 aspectFit(完整显示) vs aspectFill(填充容器裁剪超出)。

查看源码
vue
<template>
    <view class="row">
        <ux-image src="https://placehold.co/600x200.png" width="100" height="100" mode="aspectFit" />
        <ux-image custom-class="gap" src="https://placehold.co/600x200.png" width="100" height="100" mode="aspectFill" />
    </view>
</template>

<style scoped>
.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.gap {
    margin-left: 12px;
}
</style>

淡入动画 ​

fade 控制加载完成淡入过渡,对比 无动画 vs 500ms 淡入。

查看源码
vue
<template>
    <view class="row">
        <ux-image src="https://placehold.co/100x100.png" width="100" height="100" :fade="false" :lazy-load="false" />
        <ux-image custom-class="gap" src="https://placehold.co/100x100.png" width="100" height="100" :fade="true" :duration="500" />
    </view>
</template>

<style scoped>
.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.gap {
    margin-left: 12px;
}
</style>

背景色占位 ​

通过 bg-color 设置加载/失败占位的背景色,默认使用主题背景色;示意用失败图片让占位常驻,便于对比。

查看源码
vue
<template>
    <view class="row">
        <ux-image src="https://invalid.example.com/bg-not-found.png" width="100" height="100" />
        <ux-image custom-class="gap" src="https://invalid.example.com/bg-not-found.png" width="100" height="100" :bg-color="'#f5222d'" />
    </view>
</template>

<style scoped>
.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.gap {
    margin-left: 12px;
}
</style>

自定义插槽 ​

error 插槽可完全自定义失败占位内容,也可用 loading-icon/error-icon 选用内置图标;左侧自定义、右侧加载中图标。

查看源码
vue
<template>
    <view class="row">
        <ux-image src="https://invalid.example.com/slot-not-found.png" width="100" height="100" :show-error="true">
            <template #error>
                <view class="holder">
                    <ux-icon name="info-circle" color="#f5222d" size="20" />
                    <text class="tip">自定义失败</text>
                </view>
            </template>
        </ux-image>
        <ux-image custom-class="gap" src="https://placehold.co/100x100.png?delay=3000" width="100" height="100" loading-icon="star" :show-loading="true" :show-error="false" />
    </view>
</template>

<style scoped>
.row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.gap {
    margin-left: 12px;
}
.holder {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.tip {
    font-size: 10px;
    color: #f5222d;
    margin-top: 4px;
}
</style>

API ​

Props ​

属性说明类型默认值版本
customStyle自定义根节点样式string | UTSJSONObject--
customClass自定义根节点样式类string''-
src图片地址string''-
mode裁剪模式ImgMode'aspectFit'-
width宽度string | number'100%'-
height高度string | number'auto'-
shape图片形状(词表见 types/global.uts 的 ImageShape):square-小圆角 4px(默认),circle-正圆ImageShape'square'-
borderRadius圆角值;'' 作哨兵表示未设置(传 0 即显式真直角,优先于 shape 档)string | number''-
lazyLoad是否懒加载booleantrue-
showMenuByLongpress是否开启长按图片显示识别小程序码菜单booleantrue-
loadingIcon加载中的图标string'photo'-
errorIcon加载失败的图标string'error-circle'-
showLoading是否显示加载中的图标booleantrue-
showError是否显示加载错误的图标booleantrue-
fade是否需要淡入效果booleantrue-
webp只支持网络资源,仅微信小程序生效booleanfalse-
duration过渡时间string | number500-
bgColor背景颜色string''-

Events ​

事件名说明回调参数版本
click点击图片时触发--
error图片加载失败时触发err: UniImageErrorEvent-
load图片加载成功时触发--

Slots ​

插槽名说明版本
loading自定义加载中占位内容(默认显示 loading-icon)-
error自定义加载失败占位内容(默认显示 error-icon)-

注意事项 ​

  • borderRadius 传 0 表示"显式真直角",会覆盖 shape 的圆角档位;不传则按 shape 取圆角(square 小圆角 4px / circle 正圆)。
  • showMenuByLongpress 默认 true,但仅在微信小程序生效,其余端该属性无效。
  • webp 仅微信小程序支持,且只对网络图片生效。
  • duration 只在 fade 为 true 时生效,单位为 ms。
  • 加载失败时自动切换为错误占位;如需自定义,关闭 showError 或用 error 插槽接管。