引入

import Vue from 'vue';
import { RadioGroup, Radio } from 'vant';

Vue.use(Radio);
Vue.use(RadioGroup);

代码演示

基础用法

通过v-model绑定值当前选中项的 name

<van-radio-group v-model="radio">
  <van-radio name="1">单选框 1</van-radio>
  <van-radio name="2">单选框 2</van-radio>
</van-radio-group>
export default {
  data() {
    return {
      radio: '1'
    }
  }
};

禁用状态

通过disabled属性禁止选项切换,在Radio上设置disabled可以禁用单个选项

<van-radio-group v-model="radio" disabled>
  <van-radio name="1">单选框 1</van-radio>
  <van-radio name="2">单选框 2</van-radio>
</van-radio-group>

禁用文本点击

设置label-disabled属性后,点击单选框图标以外的内容不会触发切换

<van-radio-group v-model="radio">
  <van-radio name="1" icon-disabled>单选框 1</van-radio>
  <van-radio name="2" icon-disabled>单选框 2</van-radio>
</van-radio-group>

自定义形状

通过square属性设置选中状态的图标颜色

<van-radio-group v-model="radio">
  <van-radio name="1" shape="square">单选框 1</van-radio>
  <van-radio name="2" shape="square">单选框 2</van-radio>
</van-radio-group>

自定义颜色

通过checked-color属性设置选中状态的图标颜色

<van-radio-group v-model="radio">
  <van-radio name="1" checked-color="#07c160">单选框 1</van-radio>
  <van-radio name="2" checked-color="#07c160">单选框 2</van-radio>
</van-radio-group>

自定义大小

通过icon-size属性可以自定义图标的大小

<van-radio-group v-model="radio">
  <van-radio name="1" icon-size="24px">单选框 1</van-radio>
  <van-radio name="2" icon-size="24px">单选框 2</van-radio>
</van-radio-group>

自定义图标

通过icon插槽自定义图标,并通过slotProps判断是否为选中状态

<van-radio-group v-model="radio">
  <van-radio name="1">
    单选框 1
    <img
      slot="icon"
      slot-scope="props"
      :src="props.checked ? activeIcon : inactiveIcon"
    >
  </van-radio>
  <van-radio name="2">
    单选框 2
    <img
      slot="icon"
      slot-scope="props"
      :src="props.checked ? activeIcon : inactiveIcon"
    >
  </van-radio>
</van-radio-group>
export default {
  data() {
    radio: '1',
    activeIcon: 'https://img.yzcdn.cn/vant/user-active.png',
    inactiveIcon: 'https://img.yzcdn.cn/vant/user-inactive.png'
  }
}

与 Cell 组件一起使用

此时你需要再引入Cell和CellGroup组件

<van-radio-group v-model="radio">
  <van-cell-group>
    <van-cell title="单选框 1" clickable @click="radio = '1'">
      <van-radio slot="right-icon" name="1" />
    </van-cell>
    <van-cell title="单选框 2" clickable @click="radio = '2'">
      <van-radio slot="right-icon" name="2" />
    </van-cell>
  </van-cell-group>
</van-radio-group>

API

Radio Props

name标识符any-
shape形状,可选值为 squarestringround
disabled是否为禁用状态booleanfalse
label-disabled是否禁用文本内容点击booleanfalse
label-position文本位置,可选值为 leftstringright
icon-size图标大小,默认单位为pxnumber | string20px
checked-color选中状态颜色string#1989fa

RadioGroup Props

v-model当前选中项的标识符any-
disabled是否禁用所有单选框booleanfalse
icon-size v2.2.3所有单选框的图标大小,默认单位为pxnumber | string20px
checked-color v2.2.3所有单选框的选中状态颜色string#1989fa

Radio Events

click点击单选框时触发event: Event

RadioGroup Events

change当绑定值变化时触发的事件当前选中项的 name

Radio Slots

default自定义文本-
icon自定义图标checked: 是否为选中状态


实例演示

07-05 12:52