当前位置:首页 > VUE

vue实现头像组件

2026-02-18 01:15:35VUE

Vue 头像组件实现

基础头像组件实现

创建一个基础的Vue头像组件,支持图片URL或文字占位符。

<template>
  <div class="avatar" :style="avatarStyle">
    <img v-if="src" :src="src" :alt="alt" />
    <span v-else class="initials">{{ initials }}</span>
  </div>
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      default: ''
    },
    alt: {
      type: String,
      default: 'Avatar'
    },
    name: {
      type: String,
      default: ''
    },
    size: {
      type: Number,
      default: 40
    },
    backgroundColor: {
      type: String,
      default: '#ccc'
    },
    textColor: {
      type: String,
      default: '#fff'
    }
  },
  computed: {
    initials() {
      if (!this.name) return '?'
      const parts = this.name.split(' ')
      return parts.map(part => part[0].toUpperCase()).join('')
    },
    avatarStyle() {
      return {
        width: `${this.size}px`,
        height: `${this.size}px`,
        'background-color': this.backgroundColor,
        color: this.textColor,
        'font-size': `${this.size * 0.4}px`
      }
    }
  }
}
</script>

<style scoped>
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  overflow: hidden;
  font-weight: bold;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

使用示例

<template>
  <div>
    <Avatar src="https://example.com/avatar.jpg" alt="User Avatar" />
    <Avatar name="John Doe" />
    <Avatar name="Jane Smith" :size="60" backgroundColor="#4a6fa5" />
  </div>
</template>

<script>
import Avatar from './Avatar.vue'

export default {
  components: {
    Avatar
  }
}
</script>

高级功能扩展

添加更多功能如在线状态指示、边框效果等。

<template>
  <div class="avatar-container" :style="containerStyle">
    <div class="avatar" :style="avatarStyle">
      <img v-if="src" :src="src" :alt="alt" />
      <span v-else class="initials">{{ initials }}</span>
    </div>
    <span v-if="online" class="online-status" :style="statusStyle"></span>
  </div>
</template>

<script>
export default {
  props: {
    // 基础props同上
    online: {
      type: Boolean,
      default: false
    },
    borderColor: {
      type: String,
      default: '#fff'
    },
    borderWidth: {
      type: Number,
      default: 2
    }
  },
  computed: {
    // 基础computed同上
    containerStyle() {
      return {
        width: `${this.size + this.borderWidth * 2}px`,
        height: `${this.size + this.borderWidth * 2}px`
      }
    },
    statusStyle() {
      return {
        width: `${this.size * 0.25}px`,
        height: `${this.size * 0.25}px`,
        'background-color': this.online ? '#4caf50' : '#f44336'
      }
    }
  }
}
</script>

<style scoped>
.avatar-container {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.avatar {
  border: v-bind('`${borderWidth}px solid ${borderColor}`');
}

.online-status {
  position: absolute;
  bottom: 0;
  right: 0;
  border-radius: 50%;
  border: 2px solid v-bind('borderColor');
}
</style>

性能优化

添加图片加载失败处理,使用懒加载技术。

<template>
  <div class="avatar" :style="avatarStyle">
    <img
      v-if="src && !imageError"
      :src="src"
      :alt="alt"
      @error="handleImageError"
      loading="lazy"
    />
    <span v-else class="initials">{{ initials }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageError: false
    }
  },
  methods: {
    handleImageError() {
      this.imageError = true
    }
  },
  watch: {
    src() {
      this.imageError = false
    }
  }
}
</script>

响应式设计

添加响应式大小控制,适应不同屏幕尺寸。

vue实现头像组件

<script>
export default {
  props: {
    size: {
      type: [Number, String],
      default: 'medium',
      validator: value => {
        return (
          typeof value === 'number' ||
          ['small', 'medium', 'large'].includes(value)
        )
      }
    }
  },
  computed: {
    computedSize() {
      const sizeMap = {
        small: 32,
        medium: 48,
        large: 64
      }
      return typeof this.size === 'number' ? this.size : sizeMap[this.size]
    },
    avatarStyle() {
      return {
        width: `${this.computedSize}px`,
        height: `${this.computedSize}px`
        // 其他样式
      }
    }
  }
}
</script>

标签: 组件头像
分享给朋友:

相关文章

vue怎么实现组件缓存

vue怎么实现组件缓存

Vue 实现组件缓存的方法 Vue 提供了内置组件 <keep-alive> 来实现组件缓存,避免重复渲染和销毁组件,提升性能。 基本用法 使用 <keep-alive> 包…

vue实现头像上传

vue实现头像上传

实现头像上传功能 在Vue中实现头像上传功能通常需要结合文件上传组件和后端接口处理。以下是具体实现步骤: 前端组件实现 创建文件上传组件模板部分: <template> <d…

如何判断react组件

如何判断react组件

判断 React 组件的依据 React 组件的判断通常基于其定义方式、功能特性和代码结构。以下是几种常见的判断方法: 函数组件与类组件 函数组件是通过 JavaScript 函数定义的,接收 pr…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <template…

vue实现头像修改

vue实现头像修改

Vue 实现头像修改功能 准备工作 确保项目中已安装必要依赖,如 axios(用于文件上传)和可选的图片裁剪库(如 vue-cropper)。通过 npm 或 yarn 安装: npm instal…

vue 实现通用组件

vue 实现通用组件

Vue 实现通用组件的方法 封装可复用的组件 将通用功能抽象为独立组件,通过props接收外部数据,通过events与父组件通信。例如封装按钮、输入框、弹窗等基础组件。 使用插槽增强灵活性 通过默认…