Switch 开关

表示两种相互对立的状态间的切换,多用于触发「开/关」。

基础用法

绑定 v-model 到一个 Boolean 类型的变量。 可以使用 --el-switch-on-color 属性与 --el-switch-off-color 属性来设置开关的背景色。

尺寸

Open

Open

Open

文字描述

使用active-text属性与inactive-text属性来设置开关的文字描述。 使用 inline-prompt 属性来控制文本是否显示在点内。

使用active-text属性与inactive-text属性来设置开关的文字描述。

Pay by month

Pay by month

Y
超出省略
完整展示多个内容

显示自定义图标

TIP

使用 inactive-iconactive-icon 属性来添加图标。 您可以传递组件名称的字符串(提前注册)或组件本身是一个 SVG Vue 组件。 Element Plus 提供了一组图标,您可以在 icon component 查看。

使用 inactive-iconactive-icon 属性来添加图标。 使用 inline-prompt 属性来控制图标显示在点内。


扩展的 value 类型

你可以设置 active-valueinactive-value 属性, 它们接受 BooleanStringNumber 类型的值。

禁用状态

设置disabled属性,接受一个Boolean,设置true即可禁用。

加载状态

设置loading属性,接受一个Boolean,设置true即加载中状态。

阻止切换

设置beforeChange属性,若返回 false 或者返回 Promise 且被 reject,则停止切换。

自定义动作图标 2.3.9

使用 inactive-action-iconactive-action-icon 属性来添加图标。

自定义操作图标 2.4.4

使用 active-actioninactive-action 属性来添加图标。

T

API

Attributes

属性名说明类型Default
model-value / v-model绑定值,必须等于 active-valueinactive-value,默认为 Boolean 类型boolean / string / numberfalse
disabled是否禁用booleanfalse
loading是否显示加载中booleanfalse
sizeswitch 的大小enum''
widthswitch 的宽度number / string''
inline-prompt无论图标或文本是否显示在点内,只会呈现文本的第一个字符booleanfalse
active-iconswitch 状态为 on 时所显示图标,设置此项会忽略 active-textstring / Component
inactive-iconswitch 状态为 off 时所显示图标,设置此项会忽略 inactive-textstring / Component
active-action-icon 2.3.9on状态下显示的图标组件string / Component
inactive-action-icon 2.3.9off状态下显示的图标组件string / Component
active-textswitch 打开时的文字描述string''
inactive-textswitch 的状态为 off 时的文字描述string''
active-valueswitch 状态为 on 时的值boolean / string / numbertrue
inactive-valueswitch的状态为 off 时的值boolean / string / numberfalse
active-color deprecated当在 on 状态时的背景颜色(已废弃,请使用 CSS var --el-switch-on-color )string''
inactive-color deprecatedoff 状态时的背景颜色(已废弃,使用 CSS var --el-switch-off-color )string''
border-color deprecated开关的边框颜色 ( 已废弃,使用 CSS var --el-switch-border-color )string''
nameswitch 对应的 name 属性string''
validate-event是否触发表单验证booleantrue
before-changeswitch 状态改变前的钩子, 返回 false 或者返回 Promise 且被 reject 则停止切换boolean / Function
idinput 的 idstring
tabindexinput 的 tabindexstring / number
label a11y deprecated等价于原生 input aria-label 属性string
aria-label a11y 2.7.2等价于原生 input aria-label 属性string

事件

事件名说明Type
changeswitch 状态发生变化时的回调函数Function

Switch Slots

名称说明
active-action 2.4.4自定义 active 行为
inactive-action 2.4.4自定义 inactive 行为

Exposes

方法详情Type
focus手动 focus 到 switch 组件Function

源代码

组件样式文档

贡献者