当前位置:首页 > VUE

vue实现单选增加class

2026-01-22 22:21:13VUE

Vue 实现单选增加 class

在 Vue 中实现单选并动态添加 class,可以通过绑定 v-bind:classv-on:click 来实现。以下是一个常见的实现方式:

vue实现单选增加class

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

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

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

使用计算属性优化

如果需要更复杂的逻辑,可以使用计算属性来动态生成 class:

vue实现单选增加class

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

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

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

使用组件实现复用

如果需要在多个地方复用单选逻辑,可以将其封装为组件:

<template>
  <div>
    <radio-group v-model="selectedItem" :options="items" />
  </div>
</template>

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

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

RadioGroup.vue 组件:

<template>
  <div>
    <div
      v-for="item in options"
      :key="item.id"
      @click="$emit('update:modelValue', item.id)"
      :class="{ 'active': modelValue === item.id }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: {
      type: Array,
      required: true
    },
    modelValue: {
      type: [Number, String],
      default: null
    }
  }
}
</script>

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

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

相关文章

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…