当前位置:首页 > VUE

vue实现div单选

2026-03-07 20:46:15VUE

实现思路

在Vue中实现div单选功能,可以通过数据绑定和事件处理完成。核心是维护一个选中状态变量,通过点击事件更新该变量,并利用动态类名或样式控制选中效果。

vue实现div单选

基础实现代码示例

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="selectedId = item.id"
      :class="{ 'active': selectedId === item.id }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedId: null,
      items: [
        { id: 1, text: '选项1' },
        { id: 2, text: '选项2' },
        { id: 3, text: '选项3' }
      ]
    }
  }
}
</script>

<style>
.active {
  background-color: #409EFF;
  color: white;
}
</style>

使用计算属性优化

对于更复杂的选择逻辑,可以使用计算属性:

vue实现div单选

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="selectItem(item)"
      :class="getItemClass(item)"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItem: null,
      items: [
        { id: 1, text: '选项1' },
        { id: 2, text: '选项2' },
        { id: 3, text: '选项3' }
      ]
    }
  },
  methods: {
    selectItem(item) {
      this.selectedItem = item
    }
  },
  computed: {
    getItemClass() {
      return (item) => ({
        'active': this.selectedItem?.id === item.id
      })
    }
  }
}
</script>

封装为可复用组件

可以封装为独立组件提高复用性:

<!-- RadioDivGroup.vue -->
<template>
  <div class="radio-div-group">
    <slot :selected="selectedValue" :select="selectValue"></slot>
  </div>
</template>

<script>
export default {
  props: {
    value: { type: [String, Number, Object], default: null }
  },
  data() {
    return {
      selectedValue: this.value
    }
  },
  methods: {
    selectValue(val) {
      this.selectedValue = val
      this.$emit('input', val)
      this.$emit('change', val)
    }
  },
  watch: {
    value(newVal) {
      this.selectedValue = newVal
    }
  }
}
</script>

使用封装组件

<template>
  <radio-div-group v-model="selectedId">
    <template #default="{ selected, select }">
      <div 
        v-for="item in items" 
        :key="item.id"
        @click="select(item.id)"
        :class="{ 'active': selected === item.id }"
      >
        {{ item.text }}
      </div>
    </template>
  </radio-div-group>
</template>

<script>
import RadioDivGroup from './RadioDivGroup.vue'

export default {
  components: { RadioDivGroup },
  data() {
    return {
      selectedId: 1,
      items: [
        { id: 1, text: '选项1' },
        { id: 2, text: '选项2' },
        { id: 3, text: '选项3' }
      ]
    }
  }
}
</script>

注意事项

  • 确保每个选项有唯一标识符(id)
  • 使用v-model可以实现双向绑定
  • 样式处理推荐使用CSS类而不是内联样式
  • 对于大量数据,考虑性能优化(如虚拟滚动)

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

相关文章

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…