Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vant Switch switch


May 07, 2021 Vant


Table of contents


Introduced

import Vue from 'vue';
import { Switch } from 'vant';

Vue.use(Switch);

Code demo

Basic usage

With the selected state of the v-model binding switch, true indicates on and false indicates off

<van-switch v-model="checked" />
export default {
  data() {
    return {
      checked: true
    };
  }
};  

The state is disabled

The switch is disabled by the disabled property, and the switch is not clickable in the disabled state

<van-switch v-model="checked" disabled />

The load status

The switch is set to a load state by the loading property, and the switch in the loading state is not clickable

<van-switch v-model="checked" loading />

Custom size

Customize the size of the switch with size properties

<van-switch v-model="checked" size="24px" />

Custom colors

The active-color property represents the background color when it is turned on, and the inactive-color represents the background color when it is turned off

<van-switch v-model="checked" active-color="#07c160" inactive-color="#ee0a24" />

Asynchronous control

When an asynchronous control switch is required, you can use the value attribute and input event instead of v-model and manually handle the switch state in the input event callback function

<van-switch :value="checked" @input="onInput" />
export default {
  data() {
    return {
      checked: true
    };
  },
  methods: {
    onInput(checked) {
      Dialog.confirm({
        title: '提醒',
        message: '是否切换开关?'
      }).then(() => {
        this.checked = checked;
      });
    }
  }
}; 

Use with cells

<van-cell center title="标题">
  <van-switch v-model="checked" slot="right-icon" size="24" />
</van-cell>

Api

Props

Parameters Description Type The default
v-model The switch is selected any false
loading Whether it is a load state boolean false
disabled Whether it is disabled or not boolean false
size v2.2.11 Switch size, the default unit is px number | string 30px
active-color The background color when turned on string #1989fa
inactive-color The background color when off string white
active-value The value when it is opened any true
inactive-value The value when closed any false

Events

The name of the event Description Callback parameters
change Switch callbacks in the switch state Checked: Whether to select the switch
click v2.2.11 Triggered when clicked event: Event


Example demonstration