当前位置:首页 > 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 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选功能。单选按钮的 v…

vue循环数组实现

vue循环数组实现

vue循环数组的实现方法 在Vue中循环数组主要通过v-for指令实现,以下是几种常见用法: 基本数组循环 使用v-for指令遍历数组,可以通过(item, index)的语法获取当前项和索引值:…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout()…