当前位置:首页 > VUE

vue radio组件实现

2026-02-11 03:18:03VUE

Vue Radio 组件实现方法

基础实现方式

使用原生 HTML 的 input 元素结合 Vue 的 v-model 指令可以快速实现单选功能。通过 v-model 绑定同一个变量,确保单选效果。

<template>
  <div>
    <input type="radio" id="option1" value="Option1" v-model="selectedOption">
    <label for="option1">Option 1</label>

    <input type="radio" id="option2" value="Option2" v-model="selectedOption">
    <label for="option2">Option 2</label>

    <p>Selected: {{ selectedOption }}</p>
  </div>
</template>

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

组件化封装

将单选按钮封装为可复用的组件,通过 props 接收选项列表和当前值,通过 emit 通知父组件选择变化。

<!-- RadioGroup.vue -->
<template>
  <div>
    <div v-for="option in options" :key="option.value">
      <input 
        type="radio" 
        :id="option.value" 
        :value="option.value" 
        v-model="internalValue"
        @change="$emit('update:modelValue', internalValue)"
      >
      <label :for="option.value">{{ option.label }}</label>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: {
      type: Array,
      required: true
    },
    modelValue: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      internalValue: this.modelValue
    }
  },
  watch: {
    modelValue(newVal) {
      this.internalValue = newVal
    }
  }
}
</script>

使用封装组件

父组件中使用封装好的单选组件,通过 v-model 进行双向绑定。

vue radio组件实现

<template>
  <RadioGroup 
    v-model="selectedOption"
    :options="[
      { value: 'opt1', label: 'Option 1' },
      { value: 'opt2', label: 'Option 2' }
    ]"
  />
  <p>Selected: {{ selectedOption }}</p>
</template>

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

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

UI 库集成

对于需要更丰富样式和功能的场景,可以直接使用 UI 库提供的单选组件。

Element UI 示例:

vue radio组件实现

<template>
  <el-radio-group v-model="radio">
    <el-radio :label="1">Option 1</el-radio>
    <el-radio :label="2">Option 2</el-radio>
  </el-radio-group>
</template>

<script>
export default {
  data() {
    return {
      radio: 1
    }
  }
}
</script>

Ant Design Vue 示例:

<template>
  <a-radio-group v-model:value="value">
    <a-radio value="A">Option A</a-radio>
    <a-radio value="B">Option B</a-radio>
  </a-radio-group>
</template>

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

自定义样式

通过 CSS 自定义单选按钮样式,通常需要隐藏原生输入框,使用伪元素创建自定义外观。

<template>
  <div class="custom-radio">
    <input 
      type="radio" 
      id="custom1" 
      value="custom1" 
      v-model="customOption"
    >
    <label for="custom1">Custom Style</label>
  </div>
</template>

<style>
.custom-radio input[type="radio"] {
  opacity: 0;
  position: absolute;
}

.custom-radio label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

.custom-radio label:before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 18px;
  height: 18px;
  border: 2px solid #ddd;
  border-radius: 50%;
}

.custom-radio input[type="radio"]:checked + label:after {
  content: "";
  position: absolute;
  left: 5px;
  top: 5px;
  width: 12px;
  height: 12px;
  background: #409eff;
  border-radius: 50%;
}
</style>

标签: 组件vue
分享给朋友:

相关文章

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template&…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现绘图

vue实现绘图

Vue 实现绘图的方法 在 Vue 中实现绘图功能,可以通过多种方式实现,包括使用原生 HTML5 Canvas、第三方库(如 Fabric.js、Konva.js)或 SVG。以下是几种常见的实现方…

vue 实现穿透

vue 实现穿透

Vue 样式穿透的实现方法 在 Vue 中,样式穿透通常指在带有 scoped 属性的样式块中,强制影响子组件的样式。以下是几种常见的实现方式: 使用 >>> 或 /deep/ 选…

vue实现肢体识别

vue实现肢体识别

Vue 中实现肢体识别的方法 在 Vue 项目中实现肢体识别通常需要结合第三方库或 API,例如 TensorFlow.js、MediaPipe 或 OpenCV。以下是几种常见的实现方式: 使用…