Switch Switch
Toggle between two states with an optional asynchronous guard.
Import
Use <hui-switch> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
<template>
<view>
<hui-switch
v-model="enabled"
:before-change="guard"
@update:model-value="event0"
@change="event1"
/>
<text>{{ enabled }}</text>
<hui-switch :model-value="true" loading /><hui-switch :model-value="true" disabled /><hui-switch
:model-value="true"
readonly
/>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('No event yet')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const enabled = ref<string | number | boolean>(false)
const guard = async () => {
await Promise.resolve()
return true
}
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:modelValue' + ': ' + [describe(arg0)].join(', ')
}
const event1 = (arg0: unknown) => {
lastEvent.value = 'change' + ': ' + [describe(arg0)].join(', ')
}
</script>
The switch remains busy while beforeChange is pending. False, rejection or unmounting prevents the stale change. Use activeValue/inactiveValue to map strings or numbers.
A value matching neither activeValue nor inactiveValue is displayed as off and produces one development warning. The supplied value is preserved.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue | SwitchValue | undefined | Controlled value; undefined means omitted. |
defaultValue | SwitchValue | undefined | Initial uncontrolled value; ignored in controlled mode. |
activeValue | SwitchValue | true | Value representing the active state. |
inactiveValue | SwitchValue | false | Value representing the inactive state. |
activeText | string | — | Text for the active state. |
inactiveText | string | — | Text for the inactive state. |
loading | boolean | false | Loading state. |
disabled | boolean | false | Disable interaction. |
readonly | boolean | undefined | Read-only presentation without value changes. |
size | ComponentSize | — | Component size; inherits configuration when omitted. |
activeColor | string | — | Color of the active item. |
inactiveColor | string | — | Color of inactive items. |
beforeChange | ((value: SwitchValue) => unknown) | — | Change guard; duplicate changes are ignored while pending. |
ariaLabel | string | — | Accessible name. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
update:modelValue | (value: string | number | boolean) | Request a controlled value update. |
change | (value: string | number | boolean) | Emitted after the value changes. |
Slots
None.
Methods
None.
Types
SwitchProps、SwitchValue、SwitchEmits are available from @nscode/handy-uni.
export type SwitchValue = string | number | boolean
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.