当前位置:首页 > 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>

响应式设计

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

<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 组件 Vue 组件可以通过单文件组件(.vue 文件)或直接在 JavaScript 中定义。以下是两种常见实现方式。 单文件组件方式 单文件组件包含模板、脚本和样式三部分,适合复杂项…

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

uniapp如何将底部导航组件化

uniapp如何将底部导航组件化

在 uniapp 中将底部导航组件化 将底部导航组件化可以提高代码复用性和维护性。以下是具体实现方法: 创建自定义底部导航组件 新建一个组件文件,例如 tab-bar.vue,放置在 comp…

vue实现组件

vue实现组件

Vue 实现组件的方法 Vue 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用…

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…