Skip to content

Switch 开关

用于在两种互斥状态(开启/关闭)之间进行即时切换的控制开关,常用于设置面板与选项控制。


代码示例

1. 基础用法 (Basic)

使用 v-model 绑定 Boolean 开关状态。

vue
<template>
  <mu-switch v-model="enabled" />
</template>

<script setup>
import { ref } from 'vue'
const enabled = ref(true)
</script>

2. 带 Label 标签 (Label)

设置 label 属性直接在开关右侧显示对应的标题说明。

vue
<template>
  <mu-switch v-model="autoUpdate" label="自动更新应用程序" />
</template>

3. 自定义激活与未激活背景色 (Custom Colors)

使用 active-colorinactive-color 分别指定开启和关闭状态下的轨道背景颜色。

vue
<template>
  <!-- 成功绿背景 -->
  <mu-switch v-model="enabled" active-color="#10B981" />

  <!-- 警告黄背景 -->
  <mu-switch v-model="enabled" active-color="#F59E0B" />
</template>

4. 禁用状态 (Disabled)

设置 disabled 禁止用户更改开关状态。

vue
<template>
  <mu-switch :model-value="true" disabled />
  <mu-switch :model-value="false" disabled />
</template>

API 属性说明 (Props)

参数名说明类型可选值默认值
v-model当前开关状态Booleantrue | falsefalse
label开关右侧描述标签文本String-''
activeColor开启状态下的轨道背景颜色String-''
inactiveColor关闭状态下的轨道背景颜色String-''
disabled是否禁用点击交互Booleantrue | falsefalse

事件说明 (Events)

事件名说明回调参数
change开关状态发生改变时触发(value: Boolean)
update:modelValue双向绑定更改变(value: Boolean)

基于 MIT 协议开源发行

H5 模拟器 (switch)☀️🔄