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); // 显示当前所做选项 } }
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 和 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>
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
name | 标识符 | any | - |
shape | 形状,可选值为 square | string | round |
disabled | 是否为禁用状态 | boolean | false |
label-disabled | 是否禁用文本内容点击 | boolean | false |
label-position | 文本位置,可选值为 left | string | right |
icon-size | 图标大小,默认单位为 px | number | string | 20px |
checked-color | 选中状态颜色 | string | #1989fa |
参数 | 说明 | 类型 | 默认值 |
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 |
事件名 | 说明 | 回调参数 |
---|---|---|
click | 点击单选框时触发 | event: Event |
事件名 | 说明 | 回调参数 |
---|---|---|
change | 当绑定值变化时触发的事件 | 当前选中项的 name |
名称 | 说明 | SlotProps |
---|---|---|
default | 自定义文本 | - |
icon | 自定义图标 | checked: 是否为选中状态 |
用于页面中展示重要的提示信息。基本用法页面中的非浮层元素,不会自动消失。Alert 组件提供四种主题,由type属性指定,默认值为...
NavMenu 导航菜单为网站提供导航功能的菜单。顶栏适用广泛的基础用法。导航菜单默认为垂直模式,通过mode属性可以使导航菜单变更...
在Bootstrap4中,可以使用.nav类来创建导航,简单的水平导航如果你想创建一个简单的水平导航栏,可以在ul元素上添加.nav类,在每...
导航栏是一个将商标、导航以及别的元素简单放置到一个简洁导航页头的的集合。在Bootstrap4中,导航栏的创建并不难,而且,可以使...