import{o as a,a as t,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=n(`
Used to switch between open and closed states.
Register component globally via app.use
, refer to Component Registration for more registration ways.
import { createApp } from 'vue';
import { Switch } from 'vant';
const app = createApp();
app.use(Switch);
<van-switch v-model="checked" />
import { ref } from 'vue';
export default {
setup() {
const checked = ref(true);
return { checked };
},
};
<van-switch v-model="checked" disabled />
<van-switch v-model="checked" loading />
<van-switch v-model="checked" size="24px" />
<van-switch v-model="checked" active-color="#ee0a24" inactive-color="#dcdee0" />
Using node
slot to custom the content of the node.
<van-switch v-model="checked">
<template #node>
<div class="icon-wrapper">
<van-icon :name="checked ? 'success' : 'cross'" />
</div>
</template>
</van-switch>
<style>
.icon-wrapper {
display: flex;
width: 100%;
justify-content: center;
font-size: 18px;
}
.icon-wrapper .van-icon-success {
line-height: 32px;
color: var(--van-blue);
}
.icon-wrapper .van-icon-cross {
line-height: 32px;
color: var(--van-gray-5);
}
</style>
<van-switch :model-value="checked" @update:model-value="onUpdateValue" />
import { ref } from 'vue';
import { Dialog } from 'vant';
export default {
setup() {
const checked = ref(true);
const onUpdateValue = (newValue) => {
Dialog.confirm({
title: 'Confirm',
message: 'Are you sure to toggle switch?',
}).then(() => {
checked.value = newValue;
});
};
return {
checked,
onUpdateValue,
};
},
};
<van-cell center title="Title">
<template #right-icon>
<van-switch v-model="checked" size="24" />
</template>
</van-cell>
Attribute | Description | Type | Default |
---|---|---|---|
v-model | Check status of Switch | ActiveValue | InactiveValue | false |
loading | Whether to show loading icon | boolean | false |
disabled | Whether to disable switch | boolean | false |
size | Size of switch | number | string | 30px |
active-color | Background color when active | string | #1989fa |
inactive-color | Background color when inactive | string | white |
active-value | Value when active | any | true |
inactive-value | Value when inactive | any | false |
Event | Description | Parameters |
---|---|---|
change | Emitted when check status changed | value: any |
click | Emitted when component is clicked | event: MouseEvent |
Name | Description | SlotProps |
---|---|---|
node v3.5.0 | Custom the content of node | - |
background v3.5.0 | Custom the background of switch | - |
The component exports the following type definitions:
import type { SwitchProps } from 'vant';
The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.
Name | Default Value | Description |
---|---|---|
--van-switch-size | 30px | - |
--van-switch-width | 2em | - |
--van-switch-height | 1em | - |
--van-switch-node-size | 1em | - |
--van-switch-node-background-color | var(--van-white) | - |
--van-switch-node-box-shadow | 0 3px 1px 0 rgba(0, 0, 0, 0.05) | - |
--van-switch-background-color | var(--van-background-color-light) | - |
--van-switch-on-background-color | var(--van-primary-color) | - |
--van-switch-transition-duration | var(--van-animation-duration-base) | - |
--van-switch-disabled-opacity | var(--van-disabled-opacity) | - |
--van-switch-border | var(--van-border-width-base) solid rgba(0, 0, 0, 0.1) | - |