Skip to content

Switch 开关

开关组件,用于在两种状态之间进行切换。

基础用法

最简单的开关形式,通过 v-model 进行双向绑定。

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked1"></u-switch>
    <u-switch v-model="checked2"></u-switch>
  </div>
</template>

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

  const checked1 = ref(false);
  const checked2 = ref(true);
</script>

不同颜色

开关支持多种主题颜色:primary、success、warning、error。

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked1" color="primary"></u-switch>
    <u-switch v-model="checked2" color="success"></u-switch>
    <u-switch v-model="checked3" color="warning"></u-switch>
    <u-switch v-model="checked4" color="error"></u-switch>
  </div>
</template>

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

  const checked1 = ref(true);
  const checked2 = ref(true);
  const checked3 = ref(true);
  const checked4 = ref(true);
</script>

不同尺寸

开关支持多种尺寸:mini、small、medium、large。

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked" size="mini"></u-switch>
    <u-switch v-model="checked" size="small"></u-switch>
    <u-switch v-model="checked" size="medium"></u-switch>
    <u-switch v-model="checked" size="large"></u-switch>
  </div>
</template>

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

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

禁用状态

通过 disabled 属性禁用开关。

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked1" disabled></u-switch>
    <u-switch v-model="checked2" disabled></u-switch>
  </div>
</template>

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

  const checked1 = ref(false);
  const checked2 = ref(true);
</script>

自定义文本

使用插槽自定义选中和未选中状态的文本。

vue
<template>
  <u-switch v-model="checked">
    <template #checked>开</template>
    <template #unchecked>关</template>
  </u-switch>
</template>

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

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

自定义图标

使用插槽自定义选中和未选中状态的图标。

vue
<template>
  <u-switch v-model="checked">
    <template #checked-icon>
      <u-icon name="check"></u-icon>
    </template>
    <template #unchecked-icon>
      <u-icon name="close"></u-icon>
    </template>
  </u-switch>
</template>

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

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

API

Switch 属性

属性说明类型可选值默认值
modelValue绑定值,使用 v-modelboolean-false
disabled是否禁用boolean-false
size开关尺寸stringmini small medium largemedium
color主题颜色stringprimary success warning error-
checked选中状态文本string--
unchecked未选中状态文本string--

Switch 事件

事件名说明回调参数
change绑定值变化时触发(value: boolean)
update:modelValue更新 modelValue(value: boolean)

Switch 插槽

名称说明
checked自定义选中状态文本
unchecked自定义未选中状态文本
checked-icon自定义选中状态图标
unchecked-icon自定义未选中状态图标