…">
当前位置:首页 > 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
分享给朋友:

相关文章

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue实现多选

vue实现多选

Vue 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <templa…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…