Skip to content

Switch 开关

开关选择器。

代码演示

基础用法

vue
<template>
  <a-switch v-model:checked="checked" />
</template>
<script setup>
import { ref } from 'vue';
const checked = ref(false);
</script>

不可用

Switch 失效状态。

vue
<template>
  <a-space direction="vertical">
    <a-switch v-model:checked="checked1" :disabled="disabled" />
    <a-button type="primary" @click="onToggle">Toggle disabled</a-button>
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const checked = ref(true);
const disabled = ref(true);
const onToggle = () => {
  disabled.value = !disabled.value;
};
</script>

两种大小

size="small" 表示小号开关。

vue
<template>
  <a-space direction="vertical">
    <a-switch v-model:checked="state.checked1" />
    <a-switch v-model:checked="state.checked2" size="small" />
  </a-space>
</template>
<script setup>
import { reactive } from 'vue';
const state = reactive({
  checked1: true,
  checked2: false,
});
</script>

加载中

标识开关操作仍在执行中。

vue
<template>
  <a-space direction="vertical">
    <a-switch v-model:checked="state.checked1" loading />
    <a-switch v-model:checked="state.checked2" size="small" loading />
  </a-space>
</template>
<script setup>
import { reactive } from 'vue';
const state = reactive({
  checked1: true,
  checked2: false,
});
</script>

API

参数说明类型默认值
autofocus组件自动获取焦点booleanfalse
checked(v-model)指定当前是否选中checkedValue | unCheckedValuefalse
checkedChildren选中时的内容string | slot
checkedValue选中时的值boolean | string | numbertrue
disabled是否禁用booleanfalse
loading加载中的开关booleanfalse
size开关大小,可选值:default smallstringdefault
unCheckedChildren非选中时的内容string | slot
unCheckedValue非选中时的值boolean | string | numberfalse

事件

事件名称说明回调参数
change变化时回调函数Function(checked: boolean | string | number, event: Event)
click点击时回调函数Function(checked: boolean | string | number, event: Event)

方法

名称描述
blur()移除焦点
focus()获取焦点