当前位置:首页 > VUE

vue实现div单选

2026-01-14 04:11:59VUE

实现 div 单选功能

在 Vue 中实现 div 单选功能可以通过多种方式完成。以下是几种常见的实现方法:

使用 v-model 和计算属性

通过 v-model 绑定一个变量,结合计算属性实现单选效果:

vue实现div单选

<template>
  <div>
    <div 
      v-for="option in options" 
      :key="option.value"
      @click="selected = option.value"
      :class="{ 'active': selected === option.value }"
    >
      {{ option.label }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selected: null,
      options: [
        { value: 'A', label: 'Option A' },
        { value: 'B', label: 'Option B' },
        { value: 'C', label: 'Option C' }
      ]
    }
  }
}
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

使用组件封装

创建一个可复用的单选组件:

<template>
  <div class="radio-group">
    <div
      v-for="item in items"
      :key="item.value"
      class="radio-item"
      :class="{ 'selected': value === item.value }"
      @click="$emit('input', item.value)"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: [String, Number],
      required: true
    },
    items: {
      type: Array,
      required: true
    }
  }
}
</script>

<style>
.radio-item {
  padding: 8px 16px;
  cursor: pointer;
  border: 1px solid #ddd;
  margin: 4px;
}

.selected {
  background-color: #42b983;
  color: white;
}
</style>

使用 v-for 和动态样式

通过 v-for 循环渲染选项,动态应用样式:

vue实现div单选

<template>
  <div>
    <div
      v-for="(item, index) in items"
      :key="index"
      class="option"
      :class="{ active: activeIndex === index }"
      @click="selectItem(index)"
    >
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: null,
      items: ['Option 1', 'Option 2', 'Option 3']
    }
  },
  methods: {
    selectItem(index) {
      this.activeIndex = index
    }
  }
}
</script>

<style>
.option {
  padding: 10px;
  margin: 5px;
  border: 1px solid #ccc;
  cursor: pointer;
}

.active {
  background-color: #42b983;
  color: white;
}
</style>

使用 Vuex 管理状态

在大型应用中,可以使用 Vuex 管理单选状态:

<template>
  <div>
    <div
      v-for="option in options"
      :key="option.value"
      @click="selectOption(option.value)"
      :class="{ 'active': $store.state.selectedOption === option.value }"
    >
      {{ option.label }}
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    options() {
      return [
        { value: 'A', label: 'Option A' },
        { value: 'B', label: 'Option B' },
        { value: 'C', label: 'Option C' }
      ]
    }
  },
  methods: {
    selectOption(value) {
      this.$store.commit('setSelectedOption', value)
    }
  }
}
</script>

使用动态组件

通过动态组件实现更复杂的选择逻辑:

<template>
  <div>
    <div
      v-for="option in options"
      :key="option.component"
      @click="currentComponent = option.component"
      :class="{ 'active': currentComponent === option.component }"
    >
      {{ option.label }}
    </div>

    <component :is="currentComponent" />
  </div>
</template>

<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'

export default {
  components: {
    ComponentA,
    ComponentB
  },
  data() {
    return {
      currentComponent: null,
      options: [
        { component: 'ComponentA', label: 'Show A' },
        { component: 'ComponentB', label: 'Show B' }
      ]
    }
  }
}
</script>

以上方法都可以实现 div 单选功能,选择哪种方式取决于具体项目需求和复杂度。简单的单选可以使用第一种方法,复杂的项目可能需要组件封装或状态管理。

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

相关文章

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:用…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rou…

vue实现复选

vue实现复选

Vue 实现复选框 在 Vue 中实现复选框可以通过 v-model 指令绑定数据,同时结合 input 元素的 type="checkbox" 属性来实现。以下是几种常见的实现方式: 单个复选框…