当前位置:首页 > 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 的响应式系统基于 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)实现,通过数据劫持和依赖收集实现数据变化时的自动更新。…

vue实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个…

vue实现中台

vue实现中台

Vue 实现中台系统的关键步骤 技术选型与基础搭建 使用 Vue 3(Composition API)或 Vue 2(Options API)作为前端框架,搭配 Vue Router 实现路由管理,V…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Reflec…