Image 图片
增强版的图片组件,提供加载中、加载失败提示、圆角、预览等功能。
基础用法
<zk-image
src="https://www.zenkuai.com/attachment/demo/3.jpg"
width="200"
height="300"
/>
代码示例
<template>
<zk-theme type="page" :padding="30" showBack navbarTitle="Image图片">
<!-- 基础用法 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="基础用法" />
<view class="image-row">
<zk-image src="https://www.zenkuai.com/attachment/demo/3.jpg" :width="200" :height="200" />
</view>
</zk-theme>
<!-- 填充模式 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="填充模式" />
<view class="image-row">
<view class="image-item" v-for="mode in modes" :key="mode">
<zk-image :src="demoImage" :mode="mode" :width="200" :height="200" />
<zk-text type="placeholder" :size="24" margin="10rpx 0 0 0" :text="mode" />
</view>
</view>
</zk-theme>
<!-- 圆形图片 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="圆形图片" />
<view class="image-row">
<zk-image :src="demoImage" :width="200" :height="200" round />
</view>
</zk-theme>
<!-- 圆角图片 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="圆角图片" />
<view class="image-row">
<zk-image :src="demoImage" :width="200" :height="200" :radius="20" />
</view>
</zk-theme>
<!-- 加载中提示 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="加载中提示" />
<view class="image-row">
<zk-image src="https://www.zenkuai.com/attachment/demo/3.jpg" :width="200" :height="200"
loading-text="自定义提示" />
</view>
</zk-theme>
<!-- 加载失败 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="加载失败" />
<view class="image-row">
<zk-image src="https://www.zenkuai.com/attachment/weixin1.jpg" :width="200" :height="200"
error-text="加载失败" />
</view>
</zk-theme>
<!-- 自定义背景色 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="自定义背景色" />
<view class="image-row">
<zk-image :src="demoImage" :width="200" :height="200" bg-color="#f0f0f0" />
</view>
</zk-theme>
<!-- 长按菜单 -->
<zk-theme type="card" :padding="30" :radius="16" margin="0 0 40rpx 0">
<zk-text type="secondary" :size="28" weight="bold" margin="0 0 20rpx 0" text="长按菜单" />
<view class="image-row">
<zk-image :src="demoImage" :width="200" :height="200" :show-menu-by-longpress="true" />
</view>
</zk-theme>
</zk-theme>
</template>
<script>
export default {
data() {
return {
demoImage: 'https://www.zenkuai.com/attachment/demo/3.jpg',
modes: [
'scaleToFill',
'aspectFit',
'aspectFill',
'widthFix',
'heightFix'
]
}
}
}
</script>
<style>
.image-row {
/* #ifndef APP-NVUE */
display: flex;
flex-wrap: wrap;
gap: 20rpx;
/* #endif */
/* #ifdef APP-NVUE */
flex-direction: row;
flex-wrap: wrap;
/* #endif */
}
.image-item {
/* #ifndef APP-NVUE */
display: flex;
flex-direction: column;
align-items: center;
/* #endif */
/* #ifdef APP-NVUE */
flex-direction: column;
align-items: center;
margin: 10rpx;
/* #endif */
}
</style>
API
Props 属性
参数 |
说明 |
类型 |
可选值 |
默认值 |
是否必填 |
src |
图片链接 |
string |
- |
'' |
是 |
mode |
图片填充模式 |
string |
none / fill / cover / contain / widthFix / heightFix / scaleDown |
aspectFill |
否 |
width |
图片宽度 |
string / number |
- |
'200' |
否 |
height |
图片高度 |
string / number |
- |
'300' |
否 |
round |
是否显示为圆形 |
boolean |
true / false |
false |
否 |
radius |
圆角大小 |
string / number |
- |
0 |
否 |
lazyLoad |
是否开启懒加载 |
boolean |
true / false |
true |
否 |
showMenuByLongpress |
是否开启长按图片显示识别小程序码菜单 |
boolean |
true / false |
false |
否 |
showLoading |
是否展示加载中提示 |
boolean |
true / false |
true |
否 |
loadingText |
加载中提示文案 |
string |
- |
加载中... |
否 |
showError |
是否展示加载失败提示 |
boolean |
true / false |
true |
否 |
errorText |
加载失败提示文案 |
string |
- |
加载失败 |
否 |
bgColor |
背景颜色 |
string |
- |
#f3f4f6 |
否 |
previewConfig |
预览配置,支持 uni.previewImage 的所有配置项 |
object |
- |
{} |
否 |
Events 事件
事件名称 |
说明 |
回调参数 |
click |
点击图片时触发 |
event: Event |
load |
图片加载成功时触发 |
event: Event |
error |
图片加载失败时触发 |
event: Event |