…">
当前位置:首页 > VUE

vue实现一个标签选择

2026-01-12 08:29:45VUE

Vue 标签选择实现

基础实现

创建一个基础的标签选择组件,包含标签的添加、删除和选择功能。

<template>
  <div class="tag-selector">
    <div class="selected-tags">
      <span v-for="(tag, index) in selectedTags" :key="index" class="tag">
        {{ tag }}
        <button @click="removeTag(index)">×</button>
      </span>
    </div>
    <input
      v-model="newTag"
      @keydown.enter="addTag"
      placeholder="输入标签后按回车"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedTags: [],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim() && !this.selectedTags.includes(this.newTag)) {
        this.selectedTags.push(this.newTag.trim())
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.selectedTags.splice(index, 1)
    }
  }
}
</script>

<style>
.tag-selector {
  border: 1px solid #ddd;
  padding: 8px;
  border-radius: 4px;
}
.selected-tags {
  margin-bottom: 8px;
}
.tag {
  display: inline-block;
  background: #eee;
  padding: 4px 8px;
  margin-right: 4px;
  border-radius: 4px;
}
.tag button {
  background: none;
  border: none;
  cursor: pointer;
  margin-left: 4px;
}
input {
  border: none;
  outline: none;
  width: 100%;
}
</style>

带自动补全功能

实现带自动补全的标签选择器,提供候选标签列表。

<template>
  <div class="tag-autocomplete">
    <div class="selected-tags">
      <span v-for="(tag, index) in selectedTags" :key="index" class="tag">
        {{ tag }}
        <button @click="removeTag(index)">×</button>
      </span>
    </div>
    <input
      v-model="searchQuery"
      @input="filterTags"
      @keydown.enter="addTag"
      placeholder="输入标签"
    />
    <ul v-if="filteredTags.length" class="suggestions">
      <li
        v-for="(tag, index) in filteredTags"
        :key="index"
        @click="selectTag(tag)"
      >
        {{ tag }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedTags: [],
      allTags: ['Vue', 'React', 'Angular', 'JavaScript', 'TypeScript'],
      filteredTags: [],
      searchQuery: ''
    }
  },
  methods: {
    filterTags() {
      if (this.searchQuery) {
        this.filteredTags = this.allTags.filter(tag =>
          tag.toLowerCase().includes(this.searchQuery.toLowerCase())
        )
      } else {
        this.filteredTags = []
      }
    },
    selectTag(tag) {
      if (!this.selectedTags.includes(tag)) {
        this.selectedTags.push(tag)
      }
      this.searchQuery = ''
      this.filteredTags = []
    },
    addTag() {
      if (
        this.searchQuery.trim() &&
        !this.selectedTags.includes(this.searchQuery)
      ) {
        this.selectedTags.push(this.searchQuery.trim())
        this.searchQuery = ''
        this.filteredTags = []
      }
    },
    removeTag(index) {
      this.selectedTags.splice(index, 1)
    }
  }
}
</script>

<style>
.tag-autocomplete {
  position: relative;
  border: 1px solid #ddd;
  padding: 8px;
  border-radius: 4px;
}
.selected-tags {
  margin-bottom: 8px;
}
.tag {
  display: inline-block;
  background: #eee;
  padding: 4px 8px;
  margin-right: 4px;
  border-radius: 4px;
}
.tag button {
  background: none;
  border: none;
  cursor: pointer;
  margin-left: 4px;
}
input {
  border: none;
  outline: none;
  width: 100%;
}
.suggestions {
  position: absolute;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #ddd;
  list-style: none;
  padding: 0;
  margin: 0;
  z-index: 10;
}
.suggestions li {
  padding: 8px;
  cursor: pointer;
}
.suggestions li:hover {
  background: #f5f5f5;
}
</style>

使用第三方库

使用第三方库如vue-tags-input可以快速实现更丰富的标签选择功能。

安装:

npm install @voerro/vue-tagsinput

使用示例:

vue实现一个标签选择

<template>
  <tags-input
    v-model="tags"
    :existing-tags="existingTags"
    :typeahead="true"
    placeholder="添加标签"
  />
</template>

<script>
import TagsInput from '@voerro/vue-tagsinput'

export default {
  components: { TagsInput },
  data() {
    return {
      tags: [],
      existingTags: [
        { key: 'vue', value: 'Vue' },
        { key: 'react', value: 'React' },
        { key: 'angular', value: 'Angular' }
      ]
    }
  }
}
</script>

注意事项

  • 标签去重处理很重要,避免重复添加相同标签
  • 考虑添加标签数量限制,防止用户添加过多标签
  • 移动端适配需要考虑触摸事件的处理
  • 性能优化:当标签列表很大时,考虑虚拟滚动或分页加载

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

相关文章

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现组件

vue实现组件

Vue 组件实现基础 Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。 单文件组件 (SFC) 示例 <template>…

vue实现产品搜索

vue实现产品搜索

实现产品搜索功能 在Vue中实现产品搜索功能需要结合前端界面和后端数据处理。以下是实现的基本思路和代码示例: 数据准备 创建一个产品数据数组,包含需要搜索的产品信息: data() { ret…

vue实现数据联动

vue实现数据联动

Vue 数据联动实现方法 数据联动在 Vue 中通常指多个组件或数据项之间的动态关联更新。以下是几种常见实现方式: 使用 v-model 和计算属性 <template> <d…

vue实现数据增加

vue实现数据增加

vue实现数据增加的几种方法 在Vue中实现数据增加通常涉及以下几个核心概念和方法: 响应式数据声明 在Vue组件中,使用data选项或ref(Vue 3)声明响应式数据: // Vue 2 da…

vue实现邮件发送

vue实现邮件发送

实现邮件发送的基本思路 在Vue.js中实现邮件发送功能通常需要结合后端服务,因为浏览器端的JavaScript无法直接发送邮件。常见的方案是通过Vue前端收集邮件数据,调用后端API完成实际发送。…