当前位置:首页 > 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 等提供的多选标签组件。

以 Element UI 为例:

<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组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keyword…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue 全景实现

vue 全景实现

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

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…