Switch 开关

Switch 开关

何时使用

  • 需要表示两种互斥状态(开/关)的切换时。

基础用法

<script setup lang="ts">
import { ref } from "vue";
const checked = ref(true);
</script>

<template>
  <Switch v-model="checked" />
</template>

更多示例

禁用状态

设置 disabled 后开关不可操作。

<script setup lang="ts">
import { ref } from "vue";
import { Switch } from "@reglow/reui";

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

<template>
  <div class="flex items-center gap-4">
    <Switch v-model="checked" disabled />
    <Switch :model-value="false" disabled />
  </div>
</template>

配合 Label 使用

通过 id 与 Label 关联,点击文字即可切换开关状态。

<script setup lang="ts">
import { ref } from "vue";
import { Switch, Label } from "@reglow/reui";

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

<template>
  <div class="flex items-center gap-2">
    <Switch id="notifications" v-model="checked" />
    <Label for="notifications">接收通知</Label>
  </div>
</template>

通过外部按钮控制

在 v-model 之外,也可以通过其他交互来修改开关状态。

<script setup lang="ts">
import { ref } from "vue";
import { Switch, Button } from "@reglow/reui";

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

<template>
  <div class="flex items-center gap-4">
    <Switch v-model="checked" />
    <span class="text-sm text-muted-foreground">{{ checked ? "开" : "关" }}</span>
    <div class="flex gap-2">
      <Button size="small" @click="checked = true">开启</Button>
      <Button size="small" variant="outline" @click="checked = false">关闭</Button>
    </div>
  </div>
</template>

导入

import { Switch } from "@reglow/reui";

Props

属性类型默认值说明
modelValuebooleanfalse开关状态(v-model)
disabledbooleanfalse禁用