当前位置:首页 > 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 grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈(如是否使用 Vue Router)。 使用 Vue Router 进行编程式导航…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…

vue实现popover

vue实现popover

Vue 实现 Popover 的方法 使用第三方库(如 Element UI、Ant Design Vue) 许多成熟的 UI 库已经内置了 Popover 组件,可以直接调用。 Element U…