Skip to content

Switch

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

基础用法

使用 v-model 绑定布尔值,可以实现开关效果。

vue
<template>
  <ImSwitch v-model="checked" />
  {{ checked ? '开' : '关' }}
</template>

<script setup>
import { ref } from 'vue';

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

尺寸

设置 size 属性可以控制开关的大小。

vue
<template>
  <ImSwitch size="18" />
  <ImSwitch size="14" />
  <ImSwitch size="12" />
</template>

文字描述

vue
<template>
  <ImSwitch activeText="Y" inactiveText="N" />
</template>

自定义颜色

vue
<template>
  <ImSwitch activeColor="#13ce66" inactiveColor="#ff4949" />
</template>

自定义图标

vue
<template>
  <ImSwitch>
    <template v-slot:action="{ checked }">
      <ImIcon :name="checked ? 'check' : 'close'" size="16px" />
    </template>
  </ImSwitch>
</template>

禁用状态

vue
<template>
  <ImSwitch disabled />
</template>

API

字段名类型描述
modelValueboolean绑定的值,默认为false
disabledboolean是否禁用,默认为false
readonlyboolean是否只读,默认为false
sizestring开关大小,默认为'16px'
activeColorstring激活状态下的颜色,默认为'var(--im-primary-color-8)'
inactiveColorstring非激活状态下的颜色,默认为'var(--im-gray-color-7)'
activeValueany激活状态下的值,默认为true
inactiveValueany非激活状态下的值,默认为false
borderColorstring边框颜色,默认为'var(--im-gray-color-7)'

基于 MIT 协议发布