For AI agents: the complete documentation index is available at https://docs.halo.run/llms.txt, the full documentation bundle is available at https://docs.halo.run/llms-full.txt, and this page is available as Markdown at https://docs.halo.run/developer-guide/plugin/api-reference/ui/base-components/switch.md.

VSwitch 开关

VSwitch 用于在两个互斥状态之间切换,例如开启/关闭某个功能。

引入

import { VSwitch } from "@halo-dev/components";

示例

基础用法

<script lang="ts" setup>
import { ref } from "vue";
import { VSwitch } from "@halo-dev/components";

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

<template>
  <div class="flex items-center gap-3">
    <VSwitch v-model="enabled" />
    <span class="text-sm text-gray-500">
      {{ enabled ? "已开启" : "已关闭" }}
    </span>
  </div>
</template>

禁用状态

<script lang="ts" setup>
import { ref } from "vue";
import { VSwitch, VSpace } from "@halo-dev/components";

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

<template>
  <VSpace>
    <VSwitch v-model="enabled" disabled />
    <VSwitch :model-value="false" disabled />
  </VSpace>
</template>

加载状态

<script lang="ts" setup>
import { ref } from "vue";
import { VSwitch } from "@halo-dev/components";

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

<template>
  <VSwitch v-model="enabled" loading />
</template>

监听变化

<script lang="ts" setup>
import { ref } from "vue";
import { Toast, VSwitch } from "@halo-dev/components";

const enabled = ref(false);

function handleChange(value: boolean) {
  Toast.success(value ? "已开启" : "已关闭");
}
</script>

<template>
  <VSwitch v-model="enabled" @change="handleChange" />
</template>

Props

属性类型默认值说明
modelValuebooleanfalse是否开启,支持 v-model
disabledbooleanfalse是否禁用
loadingbooleanfalse是否显示加载状态,加载时不可操作

Emits

事件参数说明
update:modelValueboolean开关状态变化时触发
changeboolean开关状态变化时触发