Skip to content

Switch 开关

开关用于在两种相互对立的状态之间切换,通常用于即时生效的启用、关闭、显示、隐藏等设置项。

基础用法

使用 v-model 绑定开关值。默认情况下,打开时为 true,关闭时为 false

vue
<template>
  <el-switch v-model="value" />
  <el-switch
    v-model="colorValue"
    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value = ref(true)
const colorValue = ref(false)
</script>

禁用状态

设置 disabled 后,开关不可切换。禁用状态会保留当前值,仅阻止用户操作。

vue
<template>
  <el-switch v-model="open" disabled />
  <el-switch v-model="closed" disabled />
</template>

<script setup lang="ts">
import { ref } from 'vue'

const open = ref(true)
const closed = ref(false)
</script>

文字描述

使用 active-textinactive-text 可以在开关两侧展示当前状态说明。

启用
按月付费
vue
<template>
  <el-switch
    v-model="value"
    active-text="启用"
    inactive-text="停用"
  />
</template>

行内提示

设置 inline-prompt 后,文本或图标会显示在开关内部。文字提示只展示第一个字符,适合短状态。

vue
<template>
  <el-switch
    v-model="value"
    inline-prompt
    active-text="开"
    inactive-text="关"
    width="56"
  />

  <el-switch
    v-model="iconValue"
    inline-prompt
    :active-icon="Check"
    :inactive-icon="Close"
    width="56"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Check, Close } from '@element-plus/icons-vue'

const value = ref(true)
const iconValue = ref(false)
</script>

显示图标

使用 active-iconinactive-icon 可以在开关外侧展示状态图标。图标会优先于对应的文字内容。

vue
<template>
  <el-switch
    v-model="value"
    :active-icon="Check"
    :inactive-icon="Close"
    active-text="启用"
    inactive-text="停用"
  />
</template>

自定义动作区

active-action-iconinactive-action-icon 可以替换圆形滑块内的图标,也可以使用 active-actioninactive-action 插槽完全自定义动作区内容。

vue
<template>
  <el-switch
    v-model="value"
    :active-action-icon="Check"
    :inactive-action-icon="Close"
  />

  <el-switch v-model="slotValue" width="56">
    <template #active-action>
      <span>开</span>
    </template>
    <template #inactive-action>
      <span>关</span>
    </template>
  </el-switch>
</template>

扩展值

通过 active-valueinactive-value 可以把开关值从布尔值扩展为字符串或数字。v-model 的值必须与其中一个值匹配。

启用
enabled
vue
<template>
  <el-switch
    v-model="value"
    active-value="enabled"
    inactive-value="disabled"
    active-text="启用"
    inactive-text="停用"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value = ref('enabled')
</script>

加载状态

设置 loading 后,开关会显示加载状态并临时禁用交互,适合等待远程保存结果的场景。

保存中
vue
<template>
  <el-switch v-model="value" loading />
  <el-switch
    v-model="textValue"
    loading
    active-text="保存中"
    inactive-text="保存中"
  />
</template>

阻止切换

before-change 会在状态切换前执行。返回 false 或返回一个最终 rejected / false 的 Promise 时,会阻止本次切换。

可控开关
vue
<template>
  <el-switch
    v-model="value"
    :before-change="beforeChange"
    active-text="可控开关"
    inactive-text="可控开关"
  />

  <el-button size="small" @click="allowChange = !allowChange">
    {{ allowChange ? '当前允许切换' : '当前阻止切换' }}
  </el-button>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value = ref(false)
const allowChange = ref(true)

const beforeChange = () => allowChange.value
</script>

不同尺寸

size 支持 largedefaultsmall 三种尺寸,用于适配不同密度的表单或设置面板。

Large
Default
Small
vue
<template>
  <el-switch v-model="large" size="large" />
  <el-switch v-model="defaultValue" />
  <el-switch v-model="small" size="small" />
</template>

API

Switch 属性

属性名说明类型默认值
model-value / v-model绑定值,需等于 active-valueinactive-valueboolean | string | numberfalse
disabled是否禁用booleanfalse
loading是否显示加载状态booleanfalse
size开关尺寸large | default | smalldefault
width开关宽度string | number''
inline-prompt图标或文字是否显示在点内,文字只渲染第一个字符booleanfalse
active-icon打开时显示的图标组件,会覆盖 active-textstring | component-
inactive-icon关闭时显示的图标组件,会覆盖 inactive-textstring | component-
active-action-icon打开时动作区显示的图标组件string | component-
inactive-action-icon关闭时动作区显示的图标组件string | component-
active-text打开时显示的文字string''
inactive-text关闭时显示的文字string''
active-value打开时的值boolean | string | numbertrue
inactive-value关闭时的值boolean | string | numberfalse
name原生 input 的 name 属性string''
validate-event是否触发表单校验booleantrue
before-change开关状态切换前的钩子,返回 false 或 rejected Promise 时阻止切换Function-
id原生 input 的 id 属性string-
tabindex原生 input 的 tabindex 属性string | number-
aria-label原生 input 的 aria-label 属性string-

Switch 事件

事件名说明类型
change绑定值变化时触发Function
update:modelValue绑定值更新时触发Function
input输入值变化时触发Function

Switch 插槽

插槽名说明
active自定义打开状态的显示内容
inactive自定义关闭状态的显示内容
active-action自定义打开状态的动作区内容
inactive-action自定义关闭状态的动作区内容

Switch 暴露

名称说明类型
focus使开关获得焦点Function
checked当前是否处于打开状态boolean

HOMEVISTA 设计规范