Skip to content

Switch 开关

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

基础用法

vue
<template>
  <ExSwitch v-model="value1" />
  <ExSwitch v-model="value2" />
</template>

文字描述

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

按年付费
按月付费
关闭
开启
vue
<template>
  <ExSwitch v-model="value3" active-text="按月付费" inactive-text="按年付费" />
  <ExSwitch v-model="value4" active-text="开启" inactive-text="关闭" />
</template>

扩展的 value 类型

设置 active-valueinactive-value 属性,接受 Boolean, StringNumber 类型的值。

vue
<template>
  <ExSwitch 
    v-model="value5" 
    active-value="100" 
    inactive-value="0" 
  />
  <ExSwitch 
    v-model="value6" 
    :active-value="1" 
    :inactive-value="0" 
  />
</template>

禁用状态

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

vue
<template>
  <ExSwitch v-model="value1" disabled />
  <ExSwitch v-model="value2" disabled />
</template>

加载中

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

vue
<template>
  <ExSwitch v-model="value7" loading />
</template>

不同尺寸

vue
<template>
  <ExSwitch v-model="value1" size="large" />
  <ExSwitch v-model="value1" />
  <ExSwitch v-model="value1" size="small" />
</template>

API

Props

参数说明类型默认值
model-value / v-model绑定值boolean | string | numberfalse
disabled是否禁用booleanfalse
loading是否显示加载中booleanfalse
sizeswitch 的大小'large' | 'default' | 'small''default'
widthswitch 的宽度(像素)number-
active-textswitch 打开时的文字描述string-
inactive-textswitch 关闭时的文字描述string-
active-valueswitch 打开时的值boolean | string | numbertrue
inactive-valueswitch 关闭时的值boolean | string | numberfalse
nameswitch 对应的 name 属性string-
validate-event输入时是否触发表单的校验booleantrue

Events

事件名说明回调参数
changeswitch 状态发生变化时的回调函数(value: boolean | string | number) => void

Slots

名称说明
active自定义打开时的内容
inactive自定义关闭时的内容