Skip to content

Switch 开关

介绍

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

导入

js
import Switch from '@/components/form/Switch.vue';

使用方法

基础用法

开关组件的基础使用方式。

vue
<template>
  <Switch v-model="value" />
</template>

<script setup lang="ts">
import Switch from '@/components/form/Switch.vue';
import { ref } from 'vue';

const value = ref(false);
</script>

禁用状态

通过设置 disabled 属性来禁用开关。

vue
<template>
  <Switch v-model="value" disabled />
</template>

加载状态

通过设置 loading 属性来显示加载状态。

vue
<template>
  <Switch v-model="value" loading />
</template>

自定义大小

通过设置 size 属性来调整开关的大小。

vue
<template>
  <Switch v-model="value" :size="80" />
</template>

自定义颜色

通过设置 color 属性来自定义开关的颜色。

vue
<template>
  <Switch v-model="value" color="danger" />
</template>

原生 Switch

通过设置 native 属性来使用原生的 Switch 组件。

vue
<template>
  <Switch v-model="value" native />
</template>

API 参考

Switch

开关组件。

Props

名称说明类型必填默认值
v-model开关是否开启booleanfalse
native是否使用原生 Switch 组件booleanfalse
color开关的主颜色string'primary'
inverseColor开关的反色string'background.switch'
dotColor开关点的颜色string'white'
loading开关是否是加载中状态booleanfalse
disabled开关是否禁用booleanfalse
impactFeedback是否有触感反馈(iOS平台)booleantrue
size开关大小number60

Events

名称说明参数
update:modelValue开关状态变化时触发boolean

主题变量

名称类型默认值说明
SwitchColorstringprimary开关主颜色
SwitchInverseColorstringbackground.switch开关反色
SwitchDotColorstringwhite开关点颜色
SwitchImpactFeedbackbooleantrue是否开启触感反馈
SwitchSizenumber60开关大小
SwitchDotPaddingnumber4开关点内边距
SwitchDotShadowstringrgba(0, 0, 0, 0.1) 2px 2px 8px 2px开关点阴影
SwitchAnimDurationnumber200动画持续时间