Skip to content
On this page

Avatar 头像

Avatar 组件可以用来代表人物或对象, 支持使用图片、图标或者文字作为 Avatar。

基础用法

使用 shape 和 size 属性来设置 Avatar 的形状和大小。

circle
square
<template>
  <div class="demo-avatar demo-basic">
    <div>
      <div class="sub-title">circle</div>
      <div class="demo-basic--circle">
        <div class="block">
          <etu-avatar :size="50" :src="circleUrl" />
        </div>
        <div v-for="size in sizeList" :key="size" class="block">
          <etu-avatar :size="size" :src="circleUrl" />
        </div>
      </div>
    </div>
    <div>
      <div class="sub-title">square</div>
      <div class="demo-basic--circle">
        <div class="block">
          <etu-avatar shape="square" :size="50" :src="squareUrl" />
        </div>
        <div v-for="size in sizeList" :key="size" class="block">
          <etu-avatar shape="square" :size="size" :src="squareUrl" />
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, toRefs } from "vue";

const state = reactive({
  circleUrl:
    "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
  squareUrl:
    "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png",
  sizeList: [28, 40, 60] as const,
});

const { circleUrl, squareUrl, sizeList } = toRefs(state);
</script>

<style scoped>
.demo-basic {
  display: flex;
  justify-content: space-around;
  text-align: center;
}
.demo-basic .sub-title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
.demo-basic .demo-basic--circle,
.demo-basic .demo-basic--square {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 28px;
}
.demo-basic .block:not(:last-child) {
  border-right: 1px solid var(--el-border-color);
}
.demo-basic .block {
  flex: 1;
  display: flex;
  justify-content: space-around;
}
.demo-basic .el-col:not(:last-child) {
  border-right: 1px solid var(--el-border-color);
}
</style>
<template>
  <div class="demo-avatar demo-basic">
    <div>
      <div class="sub-title">circle</div>
      <div class="demo-basic--circle">
        <div class="block">
          <etu-avatar :size="50" :src="circleUrl" />
        </div>
        <div v-for="size in sizeList" :key="size" class="block">
          <etu-avatar :size="size" :src="circleUrl" />
        </div>
      </div>
    </div>
    <div>
      <div class="sub-title">square</div>
      <div class="demo-basic--circle">
        <div class="block">
          <etu-avatar shape="square" :size="50" :src="squareUrl" />
        </div>
        <div v-for="size in sizeList" :key="size" class="block">
          <etu-avatar shape="square" :size="size" :src="squareUrl" />
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, toRefs } from "vue";

const state = reactive({
  circleUrl:
    "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
  squareUrl:
    "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png",
  sizeList: [28, 40, 60] as const,
});

const { circleUrl, squareUrl, sizeList } = toRefs(state);
</script>

<style scoped>
.demo-basic {
  display: flex;
  justify-content: space-around;
  text-align: center;
}
.demo-basic .sub-title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
.demo-basic .demo-basic--circle,
.demo-basic .demo-basic--square {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 28px;
}
.demo-basic .block:not(:last-child) {
  border-right: 1px solid var(--el-border-color);
}
.demo-basic .block {
  flex: 1;
  display: flex;
  justify-content: space-around;
}
.demo-basic .el-col:not(:last-child) {
  border-right: 1px solid var(--el-border-color);
}
</style>

展示类型

支持使用图片,图标或者文字作为 Avatar。

User
<template>
  <div class="box">
    <etu-avatar :size="40" icon="user"></etu-avatar>
    <etu-avatar
      :size="40"
      src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
    ></etu-avatar>
    <etu-avatar :size="40">User</etu-avatar>
  </div>
</template>

<style>
.box {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>
<template>
  <div class="box">
    <etu-avatar :size="40" icon="user"></etu-avatar>
    <etu-avatar
      :size="40"
      src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
    ></etu-avatar>
    <etu-avatar :size="40">User</etu-avatar>
  </div>
</template>

<style>
.box {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>

适应容器

当使用图片作为用户头像时,设置该图片如何在容器中展示。与 object-fit 属性一致

fill
contain
cover
none
scale-down
<template>
  <div class="demo-fit">
    <div v-for="fit in fits" :key="fit" class="block">
      <span class="title">{{ fit }}</span>
      <etu-avatar shape="square" :size="100" :fit="fit" :src="url" />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, toRefs } from "vue";

const state = reactive({
  fits: ["fill", "contain", "cover", "none", "scale-down"],
  url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
});

const { fits, url } = toRefs(state);
</script>

<style scoped>
.demo-fit {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.demo-fit .block {
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-grow: 0;
}

.demo-fit .title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
</style>
<template>
  <div class="demo-fit">
    <div v-for="fit in fits" :key="fit" class="block">
      <span class="title">{{ fit }}</span>
      <etu-avatar shape="square" :size="100" :fit="fit" :src="url" />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, toRefs } from "vue";

const state = reactive({
  fits: ["fill", "contain", "cover", "none", "scale-down"],
  url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
});

const { fits, url } = toRefs(state);
</script>

<style scoped>
.demo-fit {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.demo-fit .block {
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-grow: 0;
}

.demo-fit .title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
</style>

Avatar 属性

属性名说明类型可选值默认值
icon设置 Avatar 的图标类型,具体参考 Icon 组件string
sizeAvatar 大小number40
shapeAvatar 形状numcicrle/squarecircle
srcAvatar 图片的源地址string
fit当展示类型为图片的时候,设置图片如何适应容器enumcover/contain/fill/none/scale-downcover

Avatar 事件

事件名说明参数
error图片加载失败时触发

Avatar 插槽

名称说明
自定义默认内容