结合v-model实现数据双向绑定。以下是一个基础示例:
当前位置:首页 > VUE

vue组件实现单选按钮

2026-01-22 07:51:48VUE

实现单选按钮的基本结构

在Vue中实现单选按钮,可以使用原生HTML的<input type="radio">结合v-model实现数据双向绑定。以下是一个基础示例:

<template>
  <div>
    <label>
      <input type="radio" v-model="selectedOption" value="option1">
      Option 1
    </label>
    <label>
      <input type="radio" v-model="selectedOption" value="option2">
      Option 2
    </label>
    <p>Selected: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: ''
    }
  }
}
</script>

动态渲染单选按钮组

通过v-for动态生成单选按钮选项,适用于选项数据来自API或需要灵活配置的场景:

<template>
  <div>
    <label v-for="option in options" :key="option.value">
      <input 
        type="radio" 
        v-model="selectedOption" 
        :value="option.value"
      >
      {{ option.label }}
    </label>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { value: 'apple', label: 'Apple' },
        { value: 'banana', label: 'Banana' },
        { value: 'orange', label: 'Orange' }
      ]
    }
  }
}
</script>

自定义单选按钮组件

封装可复用的单选按钮组件,增强样式控制和功能扩展:

<!-- RadioButton.vue -->
<template>
  <label class="radio-container">
    <input 
      type="radio" 
      :checked="isChecked"
      @change="$emit('change', value)"
    >
    <span class="radio-checkmark"></span>
    <span class="radio-label">{{ label }}</span>
  </label>
</template>

<script>
export default {
  props: {
    label: String,
    value: [String, Number],
    isChecked: Boolean
  }
}
</script>

<style>
.radio-container {
  display: block;
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}
.radio-checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 16px;
  width: 16px;
  border: 1px solid #ddd;
  border-radius: 50%;
}
.radio-container input:checked ~ .radio-checkmark {
  background-color: #2196F3;
}
</style>

单选按钮组管理

使用计算属性和watch处理复杂逻辑,例如表单验证或联动操作:

<template>
  <div>
    <RadioButton 
      v-for="option in options"
      :key="option.value"
      :label="option.label"
      :value="option.value"
      :is-checked="selectedOption === option.value"
      @change="handleChange"
    />
  </div>
</template>

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

export default {
  components: { RadioButton },
  data() {
    return {
      selectedOption: null,
      options: [
        { value: 1, label: 'Low' },
        { value: 2, label: 'Medium' },
        { value: 3, label: 'High' }
      ]
    }
  },
  methods: {
    handleChange(value) {
      this.selectedOption = value
      this.$emit('input', value)
    }
  }
}
</script>

表单集成与验证

在Vue表单中配合验证库(如VeeValidate)使用单选按钮:

<template>
  <form @submit.prevent="submitForm">
    <div v-for="option in options" :key="option.value">
      <input
        type="radio"
        v-model="form.level"
        :value="option.value"
        v-validate="'required'"
        name="level"
      >
      {{ option.label }}
    </div>
    <span v-show="errors.has('level')" class="error">
      {{ errors.first('level') }}
    </span>
    <button type="submit">Submit</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      form: { level: '' },
      options: [
        { value: 'basic', label: 'Basic' },
        { value: 'pro', label: 'Professional' }
      ]
    }
  },
  methods: {
    submitForm() {
      this.$validator.validate().then(valid => {
        if (valid) {
          // 提交表单逻辑
        }
      })
    }
  }
}
</script>

vue组件实现单选按钮

标签: 单选组件
分享给朋友:

相关文章

vue实现广告组件

vue实现广告组件

Vue 实现广告组件的核心方法 数据驱动的广告内容渲染 通过 props 接收广告数据(如图片URL、跳转链接等),使用 v-bind 动态绑定属性。典型结构包含 <a> 标签嵌套 <…

vue实现单选按钮

vue实现单选按钮

使用 v-model 绑定单选按钮 在 Vue 中,可以通过 v-model 实现单选按钮的数据绑定。单选按钮组需要共享同一个 v-model 绑定的变量,并通过 value 属性区分选项。 <…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进…

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如…

vue   实现单选

vue 实现单选

实现单选的基本方法 在Vue中实现单选功能可以通过v-model绑定数据和v-for循环渲染选项。以下是一个基本示例: <template> <div> <…