Skip to content

nax-image

当前版本:0.1.9

nax-ui 图片组件(uni-app x / uvue)。 基于原生 image 封装,提供统一尺寸/圆角 API,以及加载中加载失败占位。

安装

easycom 自动生效,页面直接使用 <nax-image /> 即可。

建议同时安装主题包 uni_modules/nax-ui-theme 并在 App.uvue 引入主题变量,详见 主题接入

代码示例

基础用法
html
<nax-image
  src="https://example.com/a.jpg"
  width="120"
  height="120"
  shape="round"
></nax-image>
加载 / 失败占位

默认开启 showLoading / showError。可用插槽自定义:

html
<nax-image src="https://example.com/a.jpg" width="160" height="120">
  <template #loading>
    <text>加载中…</text>
  </template>
  <template #error>
    <text>图片走丢了</text>
  </template>
</nax-image>
懒加载
html
<nax-image
  src="https://example.com/a.jpg"
  lazy-load
  width="100%"
  height="180"
></nax-image>
基础 / mode
uvue
<nax-image :src="okSrc" width="100" height="100" mode="aspectFill"></nax-image>
<nax-image :src="okSrc" width="100" height="100" mode="aspectFit"></nax-image>
<nax-image :src="okSrc" width="100" height="100" mode="scaleToFill"></nax-image>
uts
const okSrc = ref('https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/shuijiao.jpg')
形状 shape
uvue
<nax-image :src="okSrc" width="88" height="88" shape="square"></nax-image>
<nax-image :src="okSrc" width="88" height="88" shape="round"></nax-image>
<nax-image :src="okSrc" width="88" height="88" shape="circle"></nax-image>
uts
const okSrc = ref('https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/shuijiao.jpg')
加载中 / 失败
uvue
<nax-image force-loading width="120" height="120" loading-text="加载中"></nax-image>
<nax-image :src="badSrc" width="120" height="120" :timeout="8000" error-text="加载失败" @error="onError"></nax-image>
<nax-image src="" width="120" height="120" error-text="无图片"></nax-image>
uts
const badSrc = ref('https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/nax-ui-not-found.png')

function onError() {
	// error: 加载失败
}
自定义插槽
uvue
<nax-image :src="badSrc" :timeout="8000" width="100%" height="140" shape="round">
	<template #error>
		<view class="slot-box">
			<text class="slot-box__text">自定义失败插槽</text>
		</view>
	</template>
</nax-image>
uts
const badSrc = ref('https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/nax-ui-not-found.png')
宽图(块级)
uvue
<nax-image :src="okSrc" width="100%" height="160" shape="round" mode="aspectFill" @click="onClickImage"></nax-image>
uts
const okSrc = ref('https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/shuijiao.jpg')

function onClickImage() {
	// click: 宽图
}

主题

通过 CSS 变量覆盖:

Token用途
--nax-color-bg-secondary次级背景色
--nax-color-text-placeholder占位文字色
--nax-color-text-secondary次要文字色
--nax-image-bg容器背景
--nax-image-status-bg占位层背景
--nax-image-radius图片圆角(shape=round 时,默认跟 --nax-radius-md / 3px,对齐 button)

Props

属性类型默认值说明
srcstring''图片地址
modestring'aspectFill'裁剪/缩放模式,同原生 image
widthstring'100%'宽度;纯数字按 px
heightstring'200'高度;纯数字按 px
shapestring'square'square 方形 | round 圆角 | circle 圆形
lazyLoadbooleanfalse懒加载
fadeShowbooleantrue加载完成淡入(App 等端)
webpbooleanfalse是否解析 webp(按端支持)
draggablebooleantrue是否可拖拽(Web)
showMenuByLongpressbooleanfalse长按菜单(小程序)
showLoadingbooleantrue是否展示加载占位
showErrorbooleantrue是否展示失败占位
loadingTextstring''加载文案
errorTextstring'加载失败'失败文案
timeoutnumber0加载超时(ms);0 不限制,超时未加载完成按失败处理
customClassstring''根节点扩展 class

Events

事件说明
load加载成功
error加载失败
click点击

Slots

插槽说明
default覆盖内容
loading自定义加载占位
error自定义失败占位