Skip to content

nax-switch

当前版本:0.1.5

uni-app x 开关,功能覆盖常用场景。

安装

easycom 自动生效,页面直接使用 <nax-switch /> 即可。

建议同时安装主题包 uni_modules/nax-ui-theme 并在 App.uvue 引入主题变量,详见 主题接入

代码示例

基础用法
uvue
<nax-switch v-model="checked" @change="onChange"></nax-switch>
尺寸 size
uvue
<nax-switch v-model="sizeSm" size="sm"></nax-switch>
<nax-switch v-model="sizeMd" size="md"></nax-switch>
<nax-switch v-model="sizeLg" size="lg"></nax-switch>
uts
const sizeSm = ref(true)
const sizeMd = ref(true)
const sizeLg = ref(true)
禁用 disabled
uvue
<nax-switch v-model="disabledOff" disabled></nax-switch>
<nax-switch v-model="disabledOn" disabled></nax-switch>
uts
const disabledOff = ref(false)
const disabledOn = ref(true)
加载 loading
uvue
<nax-switch v-model="loadingOff" loading></nax-switch>
<nax-switch v-model="loadingOn" loading></nax-switch>
uts
const loadingOff = ref(false)
const loadingOn = ref(true)

模拟异步提交:switch 先切换到目标值,这里立刻回滚并进入 loading,成功后再写入。

uvue
<nax-switch v-model="asyncVal" :loading="asyncBusy" @change="onAsyncChange"></nax-switch>
uts
const asyncVal = ref(false)
const asyncBusy = ref(false)

function onAsyncChange(v : boolean) {
	if (asyncBusy.value) {
		return
	}
	const target = v
	// 回滚到切换前,进入 loading
	asyncVal.value = !target
	asyncBusy.value = true
	setTimeout(() => {
		asyncVal.value = target
		asyncBusy.value = false
	}, 1200)
}
自定义颜色
uvue
<nax-switch v-model="colorA" active-color="#2080f0"></nax-switch>
<nax-switch v-model="colorB" active-color="#d03050" inactive-color="#f3f3f5"></nax-switch>
uts
const colorA = ref(true)
const colorB = ref(false)
切换震动 vibrateShort
uvue
<nax-switch v-model="vibrateVal" vibrate-short></nax-switch>
uts
const vibrateVal = ref(false)

主题

通过 CSS 变量覆盖:

Token用途
--nax-color-bg背景色
--nax-color-border边框色
--nax-color-primary主题主色
--nax-color-text-secondary次要文字色
--nax-opacity-disabled禁用透明度

Props

属性类型默认值说明
modelValuebooleanfalse开关状态(v-model)
disabledbooleanfalse禁用
loadingbooleanfalse加载中(阻止切换)
sizestring'md'sm 小 | md 中 | lg
activeColorstring''打开时轨道色;空则主题 primary
inactiveColorstring''关闭时轨道色;空则边框灰
vibrateShortbooleanfalse切换时短震动(支持端生效)
customClassstring''根节点扩展 class

Events

事件说明
update:modelValuev-model
change状态变化(boolean)