暗黑模式
Avatar 头像
手机扫码预览
01:50
组件名:rice-avatar
用于展示用户的头像
平台兼容性
uni-app x
| Android | iOS | 鸿蒙Next | 微信小程序 | h5 |
|---|---|---|---|---|
| √ | √ | √ | √ | √ |
基础用法
- 通过
src设置头像的链接 round为false时展示方形头像
html
<rice-avatar src="/static/images/avatar-square.png" />
<rice-avatar src="/static/images/avatar-square.png" :round="false"/>字符头像
通过 text 属性设置头像为字符类型,此时需要传递头像的背景色和字符的颜色,大小
html
<rice-avatar text="A" bg-color="#ffb34b" font-size="20px" />
<rice-avatar text="李" bg-color="#f0ebf7" color="#9c7e62" />
<rice-avatar text="王" bg-color="#d9e1ff" color="#0052d9" />图标头像
通过 icon 属性设置头像为 图标类型
html
<rice-avatar icon="star" bg-color="#afaae4" font-size="22px" />
<rice-avatar icon="user" bg-color="#77b1cc" font-size="22px" />默认头像
头像加载失败后会自动替换成默认头像
html
<rice-avatar />自定义圆角
通过 radius 设置头像的圆角值
html
<rice-avatar src="/static/images/avatar-square.png" radius="8px" />头像尺寸
通过 size 属性设置头像的尺寸,可以指定为large,mini 或者具体数值
html
<rice-avatar size="mini" src="/static/images/avatar-square.png" />
<rice-avatar size="52px" src="/static/images/avatar-square.png" />
<rice-avatar size="large" src="/static/images/avatar-square.png" />API
Props
| 属性名 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| src | String | 头像路径,不能为相对路径 | - |
| round | Boolean | 是否圆形 | - |
| radius | String|Number | 圆角 | - |
| size | String|Number | 头像尺寸,可以指定为large,mini 或者数值 | - |
| mode | String | 图片裁剪、缩放的模式,与 image 组件 mode参数 一致 | - |
| text | String|Number | 用文字代替图片,优先级高于 src | - |
| icon | String | 显示的图标,优先级高于 src 和 text | - |
| bg-color | String | 头像的背景颜色,一般显示文字时使用 | - |
| color | String | 文字颜色 | - |
| font-size | String|Number | 文字/icon大小 | - |
| default-url | String | 加载失败时的默认头像 | - |
| custom-style | Object | 自定义样式 | - |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| click | 点击头像时触发 | - |
| error | 头像加载失败时触发 | event: UniImageErrorEvent|null |
类型定义
组件导出如下类型
ts
// 组件类型
const avatarRef = ref<RiceAvatarComponentPublicInstance | null>(null)