Skip to content

Switch 开关

用于开启或关闭某个功能状态,是替代 Checkbox 的更直观表单控件。

交互式调试

🔧调试模式Playground

基础用法

vue
<script setup lang="ts">
import { ref } from 'vue'
const on = ref(false)
</script>

<template>
  <lk-switch v-model="on" />
  <view>状态:{{ on ? '已开启' : '已关闭' }}</view>
</template>

尺寸

vue
<template>
  <view class="demo-row" style="align-items:center">
    <lk-switch size="sm" />
    <lk-switch size="md" />
    <lk-switch size="lg" />
  </view>
</template>

自定义颜色

vue
<template>
  <view class="demo-col">
    <lk-switch :model-value="true" active-color="#22c55e" />
    <lk-switch :model-value="true" active-color="#f59e0b" />
    <lk-switch :model-value="true" active-color="#ef4444" />
  </view>
</template>

禁用与加载

vue
<template>
  <view class="demo-row">
    <lk-switch :model-value="true" disabled />
    <lk-switch :model-value="false" disabled />
    <lk-switch :model-value="true" loading />
  </view>
</template>

自定义值与切换拦截

activeValueinactiveValue 可定义真实提交值;beforeChange 返回 false 或抛错时会阻止切换。

vue
<script setup lang="ts">
import { ref } from 'vue'

const status = ref('enabled')

async function beforeChange(nextValue: string | number | boolean) {
  if (nextValue === 'disabled') {
    return false
  }
  return true
}
</script>

<template>
  <lk-switch
    v-model="status"
    active-value="enabled"
    inactive-value="disabled"
    :before-change="beforeChange"
    @before-change="value => console.log('before:', value)"
    @change-cancel="(value, reason) => console.log(value, reason)"
  />
</template>

带文字标签

vue
<script setup lang="ts">
import { ref } from 'vue'
const receive = ref(true)
const auto = ref(false)
</script>

<template>
  <lk-cell-group>
    <lk-cell title="接收通知">
      <template #value>
        <lk-switch v-model="receive" size="sm" />
      </template>
    </lk-cell>
    <lk-cell title="自动同步">
      <template #value>
        <lk-switch v-model="auto" size="sm" />
      </template>
    </lk-cell>
  </lk-cell-group>
</template>

API

Props

参数说明类型可选值默认值
modelValue绑定值,支持 v-modelboolean / string / numberfalse
activeValue开启时的值boolean / string / numbertrue
inactiveValue关闭时的值boolean / string / numberfalse
size尺寸stringsm / md / lgmd
disabled是否禁用booleanfalse
loading是否加载中booleanfalse
activeColor开启状态颜色string''
inactiveColor关闭状态颜色string''
beforeChange切换前拦截函数,返回 falsePromise<false> 阻止切换(nextValue) => boolean / Promise<boolean>null
inlinePrompt是否在开关内显示文字booleanfalse
activeText开启时内嵌文字string''
inactiveText关闭时内嵌文字string''
hapticFeedback是否开启轻震动反馈booleanfalse
prop表单字段名,配合 lk-form 联动校验string''
validateEvent值变更时是否触发表单校验booleantrue
id根节点 idstring''
customClass自定义类名string / object / array''
customStyle自定义样式string / object''

Events

事件名说明回调参数
update:modelValue状态变化时触发(value: boolean | string | number)
change状态变化回调(value: boolean | string | number)
click点击开关时触发,禁用、加载、切换中不触发(event: Event, checked: boolean)
before-change执行切换前触发(nextValue: boolean | string | number)
change-cancel切换被拦截或异常中断时触发(nextValue: boolean | string | number, reason: 'before-change' | 'error')

注意事项

TIP

事件触发顺序为 click -> before-change -> update:modelValue -> change。若 beforeChange 拦截失败,则触发 change-cancel,不会更新值。

基于 MIT 协议开源 · 粤ICP备2022114240号-3

Switch 开关

正在连接预览服务...

请先运行 pnpm run dev:h5