当前位置:首页 > VUE

vue实现tag效果

2026-02-11 09:41:43VUE

Vue 实现标签(Tag)效果的方法

基础标签组件实现

通过Vue组件封装可复用的标签样式,支持动态内容和自定义样式:

vue实现tag效果

<template>
  <span class="tag" :style="{ backgroundColor: color }">
    {{ text }}
    <span v-if="closable" class="close-icon" @click="$emit('close')">×</span>
  </span>
</template>

<script>
export default {
  props: {
    text: String,
    color: {
      type: String,
      default: '#409EFF'
    },
    closable: Boolean
  }
}
</script>

<style scoped>
.tag {
  display: inline-block;
  padding: 0 8px;
  height: 24px;
  line-height: 24px;
  border-radius: 4px;
  color: white;
  margin-right: 8px;
}
.close-icon {
  margin-left: 4px;
  cursor: pointer;
}
</style>

动态标签列表

结合v-for实现可增删的标签列表:

vue实现tag效果

<template>
  <div>
    <div v-for="(tag, index) in tags" :key="index">
      <Tag 
        :text="tag.text" 
        :color="tag.color"
        :closable="true"
        @close="removeTag(index)"
      />
    </div>
    <input 
      v-model="newTag"
      @keyup.enter="addTag"
      placeholder="输入标签后按回车"
    />
  </div>
</template>

<script>
import Tag from './Tag.vue'
export default {
  components: { Tag },
  data() {
    return {
      tags: [
        { text: 'Vue', color: '#42b983' },
        { text: 'JavaScript', color: '#f1c40f' }
      ],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim()) {
        this.tags.push({
          text: this.newTag,
          color: `#${Math.floor(Math.random()*16777215).toString(16)}`
        })
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1)
    }
  }
}
</script>

第三方库实现

使用成熟的UI库如Element UI的Tag组件:

<template>
  <el-tag
    v-for="tag in dynamicTags"
    :key="tag"
    closable
    :type="tag.type"
    @close="handleClose(tag)">
    {{ tag.name }}
  </el-tag>
</template>

<script>
export default {
  data() {
    return {
      dynamicTags: [
        { name: '标签一', type: '' },
        { name: '标签二', type: 'success' }
      ]
    }
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    }
  }
}
</script>

动画效果增强

为标签添加过渡动画:

<template>
  <transition-group name="tag-list" tag="div">
    <Tag 
      v-for="tag in tags"
      :key="tag.id"
      :text="tag.text"
      @close="removeTag(tag.id)"
    />
  </transition-group>
</template>

<style>
.tag-list-move {
  transition: transform 0.3s;
}
.tag-list-enter-active,
.tag-list-leave-active {
  transition: opacity 0.3s;
}
.tag-list-enter,
.tag-list-leave-to {
  opacity: 0;
}
</style>

标签: 效果vue
分享给朋友:

相关文章

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 push…

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…

vue实现搜索查询

vue实现搜索查询

Vue 实现搜索查询的方法 双向绑定实现实时搜索 在Vue中可以通过v-model绑定输入框的值,结合计算属性或watch监听输入变化实现实时搜索。 <template> <i…