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

标签输入框增强

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

<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 实现tag标签

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

相关文章

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 29…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSel…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…