当前位置:首页 > VUE

vue实现多标签选择

2026-01-22 17:19:32VUE

Vue 实现多标签选择

使用 v-model 和数组绑定

在 Vue 中,可以通过 v-model 绑定一个数组来实现多标签选择。适用于 checkbox 或自定义多选组件。

<template>
  <div>
    <label v-for="tag in availableTags" :key="tag">
      <input type="checkbox" v-model="selectedTags" :value="tag">
      {{ tag }}
    </label>
    <p>已选择: {{ selectedTags }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      availableTags: ['前端', '后端', '移动端', '数据库'],
      selectedTags: []
    }
  }
}
</script>

使用第三方组件库

对于更复杂的需求,可以使用现成的 UI 库如 Element UI、Ant Design Vue 等提供的多选标签组件。

vue实现多标签选择

以 Element UI 为例:

vue实现多标签选择

<template>
  <el-select v-model="selectedTags" multiple placeholder="请选择标签">
    <el-option
      v-for="tag in availableTags"
      :key="tag"
      :label="tag"
      :value="tag">
    </el-option>
  </el-select>
</template>

<script>
export default {
  data() {
    return {
      availableTags: ['Vue', 'React', 'Angular', 'Svelte'],
      selectedTags: []
    }
  }
}
</script>

自定义标签选择组件

如果需要完全自定义的交互,可以封装一个标签选择组件:

<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 
      type="text" 
      v-model="newTag"
      @keydown.enter="addTag"
      placeholder="输入标签后按回车">
  </div>
</template>

<script>
export default {
  props: {
    value: Array
  },
  data() {
    return {
      newTag: ''
    }
  },
  computed: {
    selectedTags: {
      get() { return this.value },
      set(val) { this.$emit('input', val) }
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim() && !this.selectedTags.includes(this.newTag)) {
        this.selectedTags = [...this.selectedTags, this.newTag.trim()]
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.selectedTags.splice(index, 1)
    }
  }
}
</script>

<style>
.tag {
  display: inline-block;
  margin: 0 5px 5px 0;
  padding: 3px 8px;
  background: #eee;
  border-radius: 3px;
}
</style>

使用 Vuex 管理标签状态

对于全局共享的标签数据,可以结合 Vuex 管理:

// store.js
export default new Vuex.Store({
  state: {
    selectedTags: []
  },
  mutations: {
    addTag(state, tag) {
      if (!state.selectedTags.includes(tag)) {
        state.selectedTags.push(tag)
      }
    },
    removeTag(state, index) {
      state.selectedTags.splice(index, 1)
    }
  }
})
<template>
  <div>
    <input 
      type="text"
      v-model="newTag"
      @keydown.enter="addTag">
    <div v-for="(tag, index) in selectedTags" :key="index">
      {{ tag }}
      <button @click="removeTag(index)">删除</button>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  data() {
    return {
      newTag: ''
    }
  },
  computed: {
    ...mapState(['selectedTags'])
  },
  methods: {
    ...mapMutations(['addTag', 'removeTag']),
    addTag() {
      if (this.newTag.trim()) {
        this.$store.commit('addTag', this.newTag.trim())
        this.newTag = ''
      }
    }
  }
}
</script>

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

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…