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

带自动补全功能

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

vue实现一个标签选择

<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可以快速实现更丰富的标签选择功能。

vue实现一个标签选择

安装:

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 生态中有许多成熟的轮播图组件库,例如 Swiper、Vue-Awesome-Swiper 等,它们提供了丰富的配置和响应式支持。 安装…

vue前端实现搜索

vue前端实现搜索

实现搜索功能的基本方法 在Vue中实现搜索功能通常涉及以下几个关键步骤,结合数据绑定、计算属性和方法调用来动态过滤和显示结果。 数据绑定与输入处理 使用v-model双向绑定搜索输入框的值,监听用…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contain…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…