当前位置:首页 > VUE

vue实现div单选

2026-03-28 06:57:16VUE

Vue 实现 Div 单选的方法

使用 v-model 和计算属性

通过 v-model 绑定选中的值,结合计算属性动态更新样式。

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

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

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

使用自定义组件

封装一个可复用的单选组件,通过 props$emit 实现数据传递。

<template>
  <div>
    <radio-group v-model="selected">
      <radio-item 
        v-for="item in items" 
        :key="item.id" 
        :value="item.id"
      >
        {{ item.text }}
      </radio-item>
    </radio-group>
  </div>
</template>

<script>
import RadioGroup from './RadioGroup.vue'
import RadioItem from './RadioItem.vue'

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

使用第三方库

如果需要更复杂的功能,可以使用如 v-radio 等第三方库。

<template>
  <v-radio-group v-model="selected">
    <v-radio 
      v-for="item in items" 
      :key="item.id" 
      :label="item.text" 
      :value="item.id"
    />
  </v-radio-group>
</template>

<script>
import { VRadioGroup, VRadio } from 'vue-radios'

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

动态样式绑定

通过动态绑定 classstyle 实现选中效果。

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="selected = item.id"
      :style="{
        backgroundColor: selected === item.id ? '#42b983' : '',
        color: selected === item.id ? 'white' : ''
      }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

使用事件总线

跨组件通信时,可以通过事件总线实现单选功能。

vue实现div单选

// event-bus.js
import Vue from 'vue'
export const EventBus = new Vue()
<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="selectItem(item.id)"
      :class="{ 'active': selected === item.id }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
import { EventBus } from './event-bus.js'

export default {
  data() {
    return {
      selected: null,
      items: [
        { id: 1, text: '选项1' },
        { id: 2, text: '选项2' },
        { id: 3, text: '选项3' }
      ]
    }
  },
  methods: {
    selectItem(id) {
      EventBus.$emit('item-selected', id)
    }
  },
  created() {
    EventBus.$on('item-selected', id => {
      this.selected = id
    })
  }
}
</script>

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

相关文章

vue实现贴图

vue实现贴图

Vue 实现贴图功能 在Vue中实现贴图功能通常涉及将图片动态添加到指定位置或元素上。以下是几种常见实现方式: 使用v-bind动态绑定图片 通过数据驱动的方式动态更换图片源: <templ…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…

vue实现checkbox

vue实现checkbox

Vue 实现 Checkbox 在 Vue 中实现 Checkbox 可以通过原生 HTML 的 <input type="checkbox"> 或使用 Vue 的 v-model 指令绑…