Skip to content

Switch 开关

介绍

在打开和关闭状态之间进行切换。

基础用法

通过 v-model 绑定选中状态。

html
<vd-switch v-model="checked" />
ts
const checked = ref(true);

自定义大小

通过 size 设置开关的大小。

html
<vd-switch v-model="checked" :size="20" />
ts
const checked = ref(true);

自定义颜色

通过 color 设置开关的激活颜色,inactive-color 设置开关的未激活颜色。

html
<vd-switch v-model="checked" :color="'#ff976a'" inactive-color="#ee0a24" />
ts
const checked = ref(true);

自定义图标

通过 icon 设置开关的激活图标,inactive-icon 设置开关的未激活图标。

html
<vd-switch v-model="checked" icon="select" inactive-icon="close" />
ts
const checked = ref(true);

展示加载中

通过 loading 设置开关的加载中状态。

html
<vd-switch v-model="checked" :loading="true" />
ts
const checked = ref(true);

禁用状态

通过 disabled 设置禁用状态,禁用状态下无法进行切换。

html
<vd-switch v-model="checked" disabled />
ts
const checked = ref(true);

异步切换

通过 before-change 设置事件来处理切换状态,切换时会显示加载中状态,支持 Promise返回 false抛出异常 时阻止切换。

html
<vd-switch v-model="checked" :before-change="beforeChange" />
ts
const checked = ref(true);

async function beforeClose(value: boolean) {
  // 处理异步逻辑
}

配合确认弹窗

html
<vd-switch :model-value="checked" @update:model-value="onChange" />
<vd-dialog name="demoDialog" />
ts
import { showDialog } from 'vital-design';
import { timeout } from '@vital-design/shared';

const checked = ref(true);

async function onChange(value: boolean) {
  await showDialog({
    name: 'demoDialog',
    title: '提示',
    message: '是否确认切换?',
    showCancelButton: true,
    confirm: async () => {
      await timeout(1000);
      checked.value = value;
    }
  });
}

API

Props

参数说明类型默认值
size尺寸大小string | number26
icon激活图标IconsNamestar-fill
inactive-icon未激活图标IconsName-
color激活颜色string-
inactive-color未激活颜色string-
loading加载中booleanfalse
loading-type加载中类型LoadingType-
disabled是否禁用booleanfalse
before-change切换前回调(value: boolean) => Awaitable<unknown>-

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent
change切换时触发boolean

Slots

名称说明
icon图标
loading加载中

赣ICP备2025061025号-1