Skip to content

Avatar ​

This component is generally used wherever avatars need to be displayed, such as in a personal center, or for displaying user avatars on comment list pages.

📌 Platform Differences ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

⚠️ Notes ​

Notes

  1. Priority Order

Avatar content is displayed according to the following priority:

Slot > mpAvatar (Mini Program avatar) > icon (icon) > text (text) > src (image)
  1. size Parameter

size supports both string and number types:

html
<!-- String preset values -->
<hy-avatar size="small"></hy-avatar>
<hy-avatar size="medium"></hy-avatar>
<hy-avatar size="large"></hy-avatar>

<!-- Number (unit: px) -->
<hy-avatar :size="40"></hy-avatar>
<hy-avatar :size="60"></hy-avatar>
  1. Using randomBgColor

When random background color is enabled, the avatar randomly selects from 20 preset colors:

html
<hy-avatar text="Hy" random-bg-color></hy-avatar>

You can specify a specific color index (0-19) via colorIndex:

html
<hy-avatar text="Hy" random-bg-color :color-index="5"></hy-avatar>
  1. Mini Program Avatar

mpAvatar is only effective on WeChat, QQ, and Baidu Mini Programs, and automatically retrieves the user's avatar:

html
<hy-avatar mp-avatar></hy-avatar>
  1. Default Avatar

When src fails to load, a built-in default avatar (base64 format) will be displayed; you can also customize it via defaultUrl:

html
<hy-avatar src="xxx" default-url="https://xxx.png"></hy-avatar>

🏯 Basic Usage ​

html
<template>
    <!-- Image avatar -->
    <hy-avatar src="https://pic1.imgdb.cn/item/67628833d0e0a243d4e5d22b.webp"></hy-avatar>

    <!-- Text avatar -->
    <hy-avatar text="Hy"></hy-avatar>

    <!-- Icon avatar -->
    <hy-avatar :icon="IconConfig.MINE"></hy-avatar>
</template>

Avatar Shape ​

html
<template>
    <!-- Circle (default) -->
    <hy-avatar :src="url" shape="circle"></hy-avatar>

    <!-- Rounded square -->
    <hy-avatar :src="url" shape="square"></hy-avatar>
</template>

<script setup>
    import { ref } from 'vue';
    const url = ref('https://pic1.imgdb.cn/item/67628833d0e0a243d4e5d22b.webp');
</script>

Avatar Size ​

html
<template>
    <hy-avatar :src="url" size="small"></hy-avatar>
    <hy-avatar :src="url" size="medium"></hy-avatar>
    <hy-avatar :src="url" size="large"></hy-avatar>
    <hy-avatar :src="url" :size="40"></hy-avatar>
</template>

<script setup>
    import { ref } from 'vue';
    const url = ref('https://pic1.imgdb.cn/item/67628833d0e0a243d4e5d22b.webp');
</script>

Text Avatar (Automatic Background Color) ​

html
<template>
    <hy-avatar text="H" font-size="18" random-bg-color></hy-avatar>
    <hy-avatar text="Y" font-size="18" random-bg-color></hy-avatar>
    <hy-avatar text="U" font-size="18" random-bg-color></hy-avatar>
    <hy-avatar text="I" font-size="18" random-bg-color></hy-avatar>
</template>

Custom Background Color ​

html
<template>
    <!-- Custom background color and text color -->
    <hy-avatar text="Hy" bg-color="#448aca" color="#ffffff"></hy-avatar>

    <!-- Specify a color index -->
    <hy-avatar text="Li" random-bg-color :color-index="5"></hy-avatar>
</template>

Using Icons ​

html
<template>
    <hy-avatar :icon="IconConfig.MINE_FILL" />
    <hy-avatar :icon="IconConfig.PHOTO" />
    <hy-avatar :icon="IconConfig.STAR" />
    <hy-avatar :icon="IconConfig.CUSTOMER_SERVICE" bg-color="#89c152" />
</template>

<script setup>
    import { IconConfig } from 'hy-app';
</script>

Mini Program Avatar ​

html
<template>
    <!-- Automatically retrieve the Mini Program user avatar -->
    <hy-avatar mp-avatar size="large"></hy-avatar>
</template>

Custom Content Slot ​

html
<template>
    <hy-avatar>
        <view class="custom-avatar">
            <text>Custom</text>
        </view>
    </hy-avatar>
</template>

<style lang="scss" scoped>
    .custom-avatar {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 50%;

        text {
            color: #fff;
            font-size: 24rpx;
        }
    }
</style>

Click Event ​

html
<template>
    <hy-avatar :src="url" name="avatar1" @click="onClick"></hy-avatar>
</template>

<script setup>
    import { ref } from 'vue';

    const url = ref('https://pic1.imgdb.cn/item/67628833d0e0a243d4e5d22b.webp');

    const onClick = ({ name, url }) => {
        // Preview the large image
        uni.previewImage({
            urls: [url],
            longPressActions: {
                itemList: ['Send to friend', 'Save image', 'Favorite'],
                success: function () {},
                fail: function (err) {
                    console.log(err.errMsg);
                },
            },
        });
    };
</script>

API ​

Avatar Props ​

PropDescriptionTypeDefault
srcAvatar path; if loading fails, the default avatar will be displayed (cannot be a relative path)string-
shapeAvatar shapecircle|squarecircle
sizeAvatar size; can be a specified string (large, medium, small) or a number; the default unit for numbers is pxstring|numbermedium
modeCrop mode of the avatar image, see uni-app imagestringscaleToFill
textUse text instead of an image; takes priority over srcstring-
bg-colorBackground color, generally used when displaying textstring#c0c4cc
colorText colorstring#ffffff
font-sizeFont size; the default unit for numbers is pxnumber|string18
iconIcon to displaystring-
mp-avatarShow the Mini Program avatar; only effective on Baidu, WeChat, and QQ Mini Programsbooleanfalse
random-bg-colorWhether to use a random background colorbooleanfalse
default-urlDefault avatar shown when loading fails (the component has a built-in default image)string-
color-indexIf randomBgColor is set to true and this value is configured, the color at the corresponding index will be taken from the default background color array; valid range is 0-19number0
nameComponent identifierstring-
customStyleCustom external styles to applyCSSProperties-
customClassCustom external class namestring-

Events ​

Event NameDescriptionCallback Parameters
clickAvatar is clickedconfig: IAvatarClickConfig, event: event object

Typings ​

Type Definitions
ts
interface IAvatarClickConfig {
    /** Component identifier */
    name: string;
    /** Avatar image URL */
    url?: string;
}

CSS Variables ​

VariableDescriptionDefault
--hy-avatar-circle-radiusBorder radius of circular avatarsFollows --hy-radius-circle (50%)
--hy-avatar-square-radiusBorder radius of square avatarsFollows --hy-border-margin-padding-sm (10rpx)
12:33
battery