当前位置:首页 > VUE

vue 实现tag标签

2026-02-19 09:26:44VUE

Vue 实现 Tag 标签的方法

基础实现

创建一个基础的 Tag 组件,包含标签文本和关闭按钮。可以使用 Vue 的单文件组件(SFC)方式实现。

<template>
  <div class="tag">
    <span>{{ text }}</span>
    <button @click="$emit('close')">×</button>
  </div>
</template>

<script>
export default {
  props: {
    text: {
      type: String,
      required: true
    }
  }
}
</script>

<style scoped>
.tag {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  background-color: #f0f0f0;
  border-radius: 4px;
  margin-right: 4px;
}
.tag button {
  margin-left: 4px;
  cursor: pointer;
  background: none;
  border: none;
}
</style>

动态标签列表

实现一个可以动态添加和删除标签的组件,通常用于标签输入场景。

<template>
  <div>
    <input v-model="newTag" @keyup.enter="addTag" placeholder="输入标签后按回车">
    <div class="tags-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', '前端'],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim() && !this.tags.includes(this.newTag.trim())) {
        this.tags.push(this.newTag.trim())
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1)
    }
  }
}
</script>

可编辑标签

实现一个可以点击编辑的标签组件,适合需要修改标签内容的场景。

<template>
  <div class="editable-tag">
    <span v-if="!isEditing" @click="startEditing">{{ text }}</span>
    <input
      v-else
      ref="input"
      v-model="editedText"
      @blur="finishEditing"
      @keyup.enter="finishEditing"
    >
    <button @click="$emit('close')">×</button>
  </div>
</template>

<script>
export default {
  props: {
    text: String
  },
  data() {
    return {
      isEditing: false,
      editedText: this.text
    }
  },
  methods: {
    startEditing() {
      this.isEditing = true
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    finishEditing() {
      this.isEditing = false
      if (this.editedText !== this.text) {
        this.$emit('update', this.editedText)
      }
    }
  }
}
</script>

带颜色的标签

为标签添加颜色属性,支持不同颜色的标签显示。

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

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

<style scoped>
.colored-tag {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  border-radius: 4px;
  margin-right: 4px;
  color: white;
}
.colored-tag button {
  margin-left: 4px;
  cursor: pointer;
  background: none;
  border: none;
  color: inherit;
}
</style>

标签输入框增强

实现一个更完善的标签输入框,支持键盘操作和更好的用户体验。

vue 实现tag标签

<template>
  <div class="tag-input">
    <div class="tags-container">
      <tag
        v-for="(tag, index) in tags"
        :key="index"
        :text="tag"
        @close="removeTag(index)"
      />
      <input
        v-model="newTag"
        @keydown.delete="handleDelete"
        @keyup.enter="addTag"
        @blur="addTag"
        placeholder="添加标签..."
      >
    </div>
  </div>
</template>

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

export default {
  components: { Tag },
  data() {
    return {
      tags: [],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      const tag = this.newTag.trim()
      if (tag && !this.tags.includes(tag)) {
        this.tags.push(tag)
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1)
    },
    handleDelete(e) {
      if (e.target.value === '' && this.tags.length > 0) {
        this.removeTag(this.tags.length - 1)
      }
    }
  }
}
</script>

<style scoped>
.tag-input {
  border: 1px solid #ddd;
  padding: 4px;
  border-radius: 4px;
}
.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.tag-input input {
  border: none;
  outline: none;
  padding: 4px;
  min-width: 100px;
}
</style>

这些方法涵盖了 Vue 中实现 Tag 标签的常见场景,可以根据实际需求选择或组合使用。

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

相关文章

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API)…

vue实现销售程序

vue实现销售程序

Vue实现销售程序的基本步骤 搭建Vue项目基础结构 使用Vue CLI或Vite初始化项目,安装必要依赖(如Vue Router、Vuex/Pinia、Element UI/Ant Design V…