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

使用示例:

<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实现一个标签选择

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

相关文章

vue实现分支

vue实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容…

vue 实现目录

vue 实现目录

Vue 实现目录的方法 在 Vue 项目中实现目录功能,可以通过以下几种方式实现,具体取决于需求和技术栈。 使用 Vue Router 实现路由目录 Vue Router 是 Vue.js 的官方路…

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload(…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPa…