Avatar
Displays an image, a text label or an icon, with remote-file resolution and failure fallback.
Import
Use <hui-avatar> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-space>
<hui-avatar
v-for="size in sizes"
:key="size"
custom-class="hui-space__item"
label="林"
:size="size"
@click="lastEvent = size"
/>
<hui-avatar
custom-class="hui-space__item"
shape="square"
icon="user"
alt="用户头像 / User avatar"
/>
<hui-avatar
custom-class="hui-space__item"
:src="cover"
:size="56"
alt="风景 / Landscape"
@load="lastEvent = 'load'"
@error="lastEvent = 'error'"
/> </hui-space
><text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const sizes = ['large', 'default', 'small'] as const
const cover =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB/0lEQVR4nM3QVUIUABAA0DmEgCDYgoCBqEgoiIiBSkp3d9d2dy/d3d3d3SWooBJ2YAL/XmPeER4ccyaoupKOu1M0PGma3oyTvqzT/pyzgbzzwQLtUNHFcIlepOxStOJKbJpBfIZhYtaN5Byj1DxjYoEpueg2tcScXnaXWXGPXXWfW/OAX/dI2GAjbnoqbbGVt9krOxzTu55n9rhk97nlDnjkD3kVjvgUj/mVTgSUTwVVzoRUz4XVLkTUL0U1rsQ0r8W1rie0vwQVF6KaG1ndg3rCi67lwzzlxz4TwD0XxL8QItQJE+tGSPWj5JdjlFfj0q8lZF5Pyr6ZknuLkG9CKjSjFN+hlVowyi1ZlVacamte7UNB/WNR4xNJ8zNZq52i3SGt0ymj2zmr1zWn3z1v0LNg2Lto1Ldk3L9sMrBiOrhqNrRmPrxuMbJhObppNbblRXzbRmLHK0DVk9T5GlD1JHdtAqqelO4tQNWT2vMGUPUQet8Cqh5i3ztA1UPq3wZUPeSBHUDVQxncBVQ91KE9QNVDG34PqHroIx8AVQ9j9COg6mGOfQJUPazxz4Cqhz3xBVD1cCa/Aqoe7tQ3QNXDm/4OqHr4Mz8AVY9gdh9Q9QjnfgKqHtH8L0DVI174Dah6JIt/AFWPdOkvoOqRLf8DVD3ylQNA1aNYPQRUPcq1o/83VquIElJ47QAAAABJRU5ErkJggg=='
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
src | string | — | Image URL; takes priority over file resolution. |
file | string | UniRemoteFile | — | Remote-file identifier or object, resolved through the configured adapter. |
fallbackSrc | string | string[] | — | Image fallback URL or ordered URL list. |
label | string | — | Accessible name or text label. |
icon | string | 'user' | Icon registry name. |
size | AvatarSize | — | Named component size or an explicit pixel size. |
shape | AvatarShape | — | Visual shape. |
fit | AvatarFit | 'cover' | Image fitting mode. |
alt | string | — | Accessible image name; an empty string explicitly overrides defaults. |
bgColor | string | — | Explicit background color. |
color | string | — | Text and icon color. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
click | (event: unknown) |
load | (info: unknown) |
error | (info: { tried: string[] }) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
default | — |
Methods
None.
Types
AvatarSize、AvatarShape、AvatarFit、AvatarProps、AvatarEmits.
Theme variables
| Variable | Default | Details |
|---|---|---|
--hui-avatar-size | Determined by size or AvatarGroup's size; large/default/small are 48px/40px/32px | Overrides the outer size. The image fills it; the default icon is 50% of this size and text is 40%. |
--hui-avatar-font-size | 40% of the actual avatar size | Overrides the text size independently. |
Set variables through customStyle or an ancestor style. A custom default slot controls its own content.
Platform notes
Supports H5, WeChat mini-program, App-vue and ordinary Vue 3 Web through shared platform components. Unit tests do not replace device acceptance; verify touch, screen readers, deactivation and native rendering on each target.
Device smoke checklist.