当前位置:首页 > VUE

vue实现div单选

2026-03-07 20:46:15VUE

实现思路

在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>

使用计算属性优化

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

<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>

封装为可复用组件

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

vue实现div单选

<!-- 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实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现开关

vue实现开关

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

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…