Image
Image loading, fallbacks, placeholders, aspect ratio and preview. Events from an earlier source are ignored.
Import
With uni-app easycom, use hui-image。 Standalone Vue imports @nscode/handy-uni/web HuiImage or installs the preview plugin.
Examples
Interaction and event results
Interact with the control. The latest event and its full payload are shown below. Popup selectors commit only after confirmation; cancellation keeps the previous value.
vue
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
function event0(arg0: unknown) {
result.value = 'load' + ': ' + JSON.stringify([arg0])
}
function event1(arg0: unknown) {
result.value = 'error' + ': ' + JSON.stringify([arg0])
}
function event2(arg0: unknown) {
result.value = 'click' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view>
<hui-image
:src="'https://picsum.photos/240/160'"
:fallback-src="['https://picsum.photos/240/161']"
:width="240"
:ratio="1.5"
alt="风景"
@load="event0"
@error="event1"
@click="event2"
></hui-image>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Props
| Property | Type | Default | Description |
|---|---|---|---|
src | string | undefined | — | Image URL, taking priority over file. |
file | string | UniRemoteFile | undefined | — | Application file mapped through resolveFileUrl. |
usage | "thumb" | "preview" | undefined | 'thumb' | URL resolver usage: thumb or preview. |
fallback-src | string | string[] | undefined | — | Deduplicated fallback URLs after the primary URL fails. |
alt | string | undefined | — | Alternative text and accessible name; empty marks decoration. |
fit | ImageFit | undefined | 'fill' | fill, contain, cover, none, width-fix or height-fix fitting. |
width | readonly [NumberConstructor, StringConstructor] | undefined | — | Width; numeric values use pixels. |
height | readonly [NumberConstructor, StringConstructor] | undefined | — | Height; numeric values use pixels. |
ratio | number | undefined | — | Reserve height from this aspect ratio when only width is provided. |
round | boolean | undefined | false | Use circular clipping. |
radius | readonly [NumberConstructor, StringConstructor] | undefined | — | Corner radius; numeric values use pixels. |
lazy | boolean | undefined | false | Load descendants asynchronously. |
show-loading | boolean | undefined | true | Show a loading placeholder. |
show-error | boolean | undefined | true | Show an error placeholder. |
placeholder | string | undefined | — | Placeholder for an empty value. |
error | string | undefined | — | Failure text; an empty string is an explicit override. |
preview-src-list | string[] | undefined | — | Preview URLs passed to showImagePreview on click. |
initial-index | number | undefined | — | Initial preview index, clamped to valid bounds. |
custom-class | string | undefined | '' | Additional root classes. |
custom-style | string | CSSProperties | undefined | — | Additional root styles. |
Events
| Event | Parameters | Description |
|---|---|---|
load | (info: ImageLoadInfo) | Reports load with the arguments shown in the example. |
error | (info: { tried: string[]; }) | Reports error with the arguments shown in the example. |
click | (event: unknown) | Reports click with the arguments shown in the example. |
Slots
| Slot | Description |
|---|---|
placeholder | Loading placeholder. |
error | Error placeholder; {reload} retries loading. |
Methods
| Method | Signature |
|---|---|
reload | () => void |
Types
ImageProps、ImageEmits、ImageExpose。 Public types are exported from the package entry.
Platform notes
| Scope | Behavior and validation limits |
|---|---|
| Shared implementation | Uses the platform image component, or an image element in ordinary Vue. Fit, placeholders, retries and aspect ratio are handled by the component. |
| Device checks pending | Check native load/error events, automatic height for tall images and fallbackSrc on WeChat and App; check object-fit and error placeholders on Web. |
See examples/uni-demo/smoke/Image.md for the checklist. Unit tests do not replace device acceptance.