翻譯進行中
本頁尚未翻譯為繁體中文,以下顯示簡體中文原文。範例的預設元件文字仍使用繁體中文。
Checkbox 复选框
独立切换值,或在复选组内选择一个选项。
引入
uni-app 配置 easycom 后使用 <hui-checkbox>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<template>
<view>
<hui-checkbox
v-model="checked"
label="同意条款 / Accept terms"
@update:model-value="event0"
@change="event1"
/>
<text>{{ checked }}</text>
<hui-checkbox indeterminate label="部分选中 / Mixed" /><hui-checkbox
disabled
:model-value="true"
label="禁用 / Disabled"
/><hui-checkbox readonly :model-value="true" label="只读 / Read only" />
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const checked = ref<string | number | boolean>(false)
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:modelValue' + ': ' + [describe(arg0)].join(', ')
}
const event1 = (arg0: unknown) => {
lastEvent.value = 'change' + ': ' + [describe(arg0)].join(', ')
}
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
modelValue | ChoiceValue | undefined | 受控值;undefined 表示未传,后续仍按受控/非受控规则处理。 |
trueValue | ChoiceValue | true | 单独使用时选中、未选中对应的值 |
falseValue | ChoiceValue | false | 单独使用时选中、未选中对应的值 |
value | ChoiceValue | undefined | 在组里时代表的值 |
label | string | — | 文字;也可用默认插槽 |
indeterminate | boolean | false | 半选(只影响显示) |
disabled | boolean | false | 组上设的默认给子项 |
readonly | boolean | undefined | 组上设的默认给子项 |
shape | "round" | "square" | — | 组上设的默认给子项 |
iconPosition | "left" | "right" | — | 组上设的默认给子项 |
size | ComponentSize | — | 组上设的默认给子项 |
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:modelValue | (value: string | number | boolean) | 请求父级更新受控值。 |
change | (value: string | number | boolean) | 值确认变化后触发。 |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
icon | { checked, indeterminate, disabled } | 自定义 icon 区域,覆盖默认内容。 |
default | — | 主体内容。 |
方法
无。
类型
CheckboxProps、CheckboxValue、CheckboxEmits 可从 @nscode/handy-uni 导入。
ts
export type CheckboxValue = ChoiceValue
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。