当前位置:首页 > VUE

vue实现单选增加class

2026-01-22 22:21:13VUE

Vue 实现单选增加 class

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

<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:

<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 组件:

vue实现单选增加class

<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中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <di…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…

vue实现ftp

vue实现ftp

Vue 中实现 FTP 功能 Vue 本身是一个前端框架,无法直接操作 FTP 协议(FTP 通常需要后端支持)。以下是两种常见实现方式: 前端与后端配合实现 FTP 前端通过 API 与后端通信,…

vue实现投票

vue实现投票

Vue实现投票功能 数据准备 在Vue组件的data中定义投票相关数据,包括选项列表和当前选中状态: data() { return { options: [ { id: 1…

vue怎么实现 tab

vue怎么实现 tab

Vue 实现 Tab 的方法 使用动态组件和 v-if 通过动态组件或 v-if 指令切换不同 Tab 内容,结合点击事件改变当前激活的 Tab。 <template> &l…