举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > vant 单选框 Vant Radio 单选框

vant 单选框 Vant Radio 单选框

2023-03-22 09:20 Vant中文教程

vant 单选框 Vant Radio 单选框

vant 单选框

Vant 单选框是一种常见的用户界面元素,它可以帮助用户在多个选项中进行快速选择。Vant 单选框是一种可以让用户从一组可能的选项中进行单一选择的 UI 组件,它可以帮助用户快速做出决定。

<van-radio v-model="value" :options="options" />

Vant 单选框最大的优势在于它的易用性,使用者只需要点击即可实现单一选择。此外,Vant 单选框也具有自定义样式、多选、不可用、尺寸和形状等特性,使得 Vant 单选框能够应对各种不同的场景。

<van-radio v-model="value" :options="options" size="large" shape="square" /> 

此外,Vant 单选框还具有自定义回调函数功能,当用户做出了单一选择之后,开发者就可以根据回调函数来执行相应的代码。例如:当用户做出了单一选择之后,开发者就可以根据回调函数来执行相应的代码来显示相应的信息。

 
 <van-radio v-model="value" :options="options" @change="onChange" /> 

 methods: { 
   onChange(value) { 
     console.log(value);  // 显示当前所做选项 
   } 
 } 

 

Vant Radio 单选框

引入

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"
    }
  }
};

水平排列

​​将 direction 属性设置为 horizontal 后,单选框组会变成水平排列​​

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

禁用状态

通过 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>

自定义形状

将 ​shape​ 属性设置为 ​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
    <template #icon="props">
      <img class="img-icon" :src="props.checked ? activeIcon : inactiveIcon" />
    </template>
  </van-radio>
  <van-radio name="2">
    单选框 2
    <template #icon="props">
      <img class="img-icon" :src="props.checked ? activeIcon : inactiveIcon" />
    </template>
  </van-radio>
</van-radio-group>

<style>
  .img-icon {
    height: 20px;
  }
</style>
export default {
  data() {
    return {
      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"">

      <template #right-icon>

        <van-radio name="1" />

      </template>

    </van-cell>

    <van-cell title="单选框 2" clickable @click="radio = "2"">

      <template #right-icon>

        <van-radio name="2" />

      </template>

    </van-cell>

  </van-cell-group>

</van-radio-group>

API

Radio Props

参数说明类型默认值
name标识符any-
shape形状,可选值为 squarestring

round

disabled是否为禁用状态boolean

false

label-disabled是否禁用文本内容点击boolean

false

label-position文本位置,可选值为 leftstring

right

icon-size图标大小,默认单位为 pxnumber | string20px
checked-color选中状态颜色string#1989fa

RadioGroup Props

 参数 说明 类型 默认值
 v-model 当前选中项的标识符 any -
 disabled 是否禁用所有单选框 boolean false
 direction           v2.5.0 排列方向,可选值为horizontal string vertical
 icon-size             v2.2.3
所有单选框的图标大小,默认单位为 px
 number | string 20px
 checked-color   v2.2.3 所有单选框的选中状态颜色 string #1989fa
Radio Events
事件名说明回调参数
click点击单选框时触发event: Event

RadioGroup Events

事件名说明回调参数
change当绑定值变化时触发的事件当前选中项的 name

Radio Slots

名称说明SlotProps
default自定义文本-
icon自定义图标checked: 是否为选中状态


实例演示

阅读全文
以上是名动网为你收集整理的vant 单选框 Vant Radio 单选框全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 名动网 mdwl.vip 版权所有 联系我们