Skip to content

Switch 开关 ​

表示两种相互对立的状态间的切换。

基础用法 ​

true
vue
<my-switch v-model="value" />

默认选中 ​

设置 modelValue 初始值为 true。

vue
<my-switch :model-value="true" />
<my-switch :model-value="false" />

受控模式 ​

完全受控的 modelValue。

vue
<my-switch v-model="value" />
<my-button @click="value = !value">切换</my-button>

禁用状态 ​

vue
<my-switch v-model="value" disabled />

加载状态 ​

vue
<my-switch v-model="value" loading />

尺寸 ​

vue
<my-switch v-model="value" size="large" />
<my-switch v-model="value" />  <!-- default -->
<my-switch v-model="value" size="small" />

带标签 ​

使用 label 属性和 label-position 控制标签位置。

消息通知
消息通知
vue
<!-- 右侧标签(默认) -->
<my-switch v-model="value" label="消息通知" />
<!-- 左侧标签 -->
<my-switch v-model="value" label="消息通知" label-position="left" />

带描述 ​

使用 description 属性添加辅助说明。

WiFi开启后自动连接已知网络
蓝牙与其他设备共享文件
vue
<my-switch v-model="value" label="WiFi" description="开启后自动连接已知网络" />

带图标 ​

通过 #checked-icon 和 #unchecked-icon 插槽在滑块内显示图标。

暗色模式
vue
<my-switch v-model="value" size="large">
  <template #checked-icon>  <sun-icon />   </template>
  <template #unchecked-icon><moon-icon /> </template>
</my-switch>

自定义颜色 ​

自定义色
vue
<my-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949" />

无标签 ​

完全不传 label 即为纯开关。

vue
<my-switch v-model="value" />

开关组 — 横向排列 ​

通过 my-switch-group + options 快速生成开关组。

苹果水果之王
橘子暂时缺货
香蕉富含钾元素
葡萄酿酒的原料
已选: apple, orange
vue
<my-switch-group v-model="value" :options="options" direction="horizontal" />

开关组 — 纵向排列 ​

苹果水果之王
橘子暂时缺货
香蕉富含钾元素
葡萄酿酒的原料
已选: apple
vue
<my-switch-group v-model="value" :options="options" direction="vertical" />

组件结构 ​

外层包裹 <div class="my-switch-wrapper"> 按 label-position 顺序排列 label 和开关本体。开关本体 .my-switch 内含:隐藏的原生 <input>、loading 图标 __loading、滑块核心 __core(内有开关图标 __icon)、状态文字 __text。my-switch-group 用于批量管理多个开关。

CSS 类名 ​

类名说明
.my-switch-wrapper外层包裹容器
.is-large大尺寸
.is-small小尺寸
.has-label有标签
.my-switch__label-wrap标签包裹容器
.my-switch__label标签文字
.my-switch__desc描述文字
.my-switch开关本体
.is-checked选中状态
.is-disabled禁用状态
.is-loading加载状态
.my-switch__original隐藏的原生 input
.my-switch__core滑块核心(圆形拖拽块)
.my-switch__icon滑块内图标
.my-switch__loading加载图标容器
.my-switch__loading-icon加载旋转图标
.my-switch__text状态文字(激活/关闭)
.my-switch-group开关组容器
.is-vertical纵向排列
.is-horizontal横向排列

API ​

Switch ​

属性说明类型默认值
modelValue绑定值boolean | string | numberfalse
disabled是否禁用booleanfalse
loading是否加载中booleanfalse
label标签文字string—
description辅助描述string—
labelPosition标签位置'left' | 'right''right'
size尺寸'large' | 'default' | 'small''default'
activeText激活时文字string—
inactiveText关闭时文字string—
activeValue激活时值boolean | string | numbertrue
inactiveValue关闭时值boolean | string | numberfalse
activeColor激活时颜色string—
inactiveColor关闭时颜色string—
name原生 name 属性string—
事件说明
update:modelValue值变更
change选中值变更
插槽说明
default标签文字
label自定义标签内容
checked-icon激活时滑块内图标
unchecked-icon关闭时滑块内图标

SwitchGroup ​

属性说明类型默认值
modelValue绑定值(已选值的数组)(string | number | boolean)[][]
options选项列表SwitchOption[][]
direction排列方向'horizontal' | 'vertical''vertical'
disabled整组禁用booleanfalse
size尺寸'large' | 'default' | 'small''default'
labelPosition标签位置'left' | 'right''right'
ts
interface SwitchOption {
  label: string
  value: string | number | boolean
  disabled?: boolean
  description?: string
}
事件说明
update:modelValue值变更
change选中值变更
插槽说明
default放置 my-switch 子组件