当前位置:首页 > VUE

vue实现tag标签

2026-01-19 20:57:10VUE

Vue 实现 Tag 标签

在 Vue 中实现 Tag 标签可以通过组件化的方式灵活定制样式和功能,以下是几种常见的实现方法:

基础实现

创建一个可复用的 Tag 组件,支持动态渲染标签内容和基础样式:

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

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

<style scoped>
.tag {
  display: inline-block;
  padding: 2px 8px;
  margin: 2px;
  border-radius: 4px;
  color: white;
  font-size: 12px;
}
.close {
  margin-left: 4px;
  cursor: pointer;
}
</style>

动态标签列表

通过 v-for 渲染标签列表,并支持添加/删除操作:

vue实现tag标签

<template>
  <div>
    <input v-model="newTag" @keyup.enter="addTag">
    <div class="tag-container">
      <tag 
        v-for="(tag, index) in tags" 
        :key="index"
        :text="tag"
        @close="removeTag(index)"
      />
    </div>
  </div>
</template>

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

export default {
  components: { Tag },
  data() {
    return {
      tags: ['Vue', 'JavaScript', 'CSS'],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim()) {
        this.tags.push(this.newTag.trim())
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1)
    }
  }
}
</script>

带可选择功能的标签

实现可选中状态的标签组件:

<template>
  <span 
    class="tag" 
    :class="{ 'active': active }"
    @click="$emit('update:active', !active)"
  >
    {{ text }}
  </span>
</template>

<script>
export default {
  props: {
    text: String,
    active: Boolean
  }
}
</script>

<style scoped>
.tag {
  display: inline-block;
  padding: 4px 12px;
  margin: 4px;
  border: 1px solid #ddd;
  border-radius: 16px;
  cursor: pointer;
}
.tag.active {
  background-color: #409EFF;
  color: white;
  border-color: #409EFF;
}
</style>

第三方库集成

使用现成的 UI 库快速实现标签功能:

vue实现tag标签

  1. Element UI:

    <el-tag
    v-for="tag in tags"
    :key="tag"
    closable
    @close="handleClose(tag)"
    >
    {{ tag }}
    </el-tag>
  2. Ant Design Vue:

    <a-tag
    v-for="tag in tags"
    :key="tag"
    closable
    @close="handleClose(tag)"
    >
    {{ tag }}
    </a-tag>

高级功能扩展

实现标签拖拽排序(需要配合 vuedraggable):

<draggable v-model="tags" tag="div" class="tag-container">
  <tag 
    v-for="(tag, index) in tags" 
    :key="index"
    :text="tag"
    @close="removeTag(index)"
  />
</draggable>
import draggable from 'vuedraggable'
export default {
  components: { draggable }
}

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

相关文章

vue实现多选联动

vue实现多选联动

vue实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…