vant/src/switch/README.zh-CN.md
lmx-Hexagram f36961fe29
docs: change slot to # (#5858)
* docs(SwipeCell): change "slot" to "v-slot"

* docs(SwipeCell): change `v-slot` to`#`

* docs(cell): change `slot` to `#`

* docs(checkbox): change `slot` to `v-slot`

* docs(field): change `slot` to `v-slot`

* docs(checkbox): remove wrong comment

* docs(radio): change `slot` to `#`

* docs(search): change `slot` to `#`

* docs(slider): change `slot` to `#`

* docs(switch): change `slot` to `#`

* docs(PullRefresh): change `slot` to `#`

* docs(collapse): change `slot` to `#`

* docs(panel): change `slot` to `#`

* docs(swipe): change `slot` to `#`

* docs(navbar): change `slot` to `#`

* docs(tab): change `slot` to `#`

* docs(tabber): change `slot` to `#`

* docs(TreeSelect): change `slot` to `#`

* docs(card): change `slot` to `#`

* docs(submitBar): change `slot` to `#`

* docs(sku): change `slot` to `#` unsure

* docs(cell): delete waste blank line

* docs(panel): fix indentation

* docs(PullRefresh): change "count" to "Refresh Count"

* docs(radio): delete waste blank line

* docs(search): move props above the event

* docs(submitBar): delete waste `<span>`

* docs(swipCell): delete waste blank line

* docs(tabbar): merge `<img>` into one line
2020-03-20 14:12:00 +08:00

2.4 KiB

Switch 开关

引入

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

Vue.use(Switch);

代码演示

基础用法

通过v-model绑定开关的选中状态,true表示开,false表示关

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

禁用状态

通过disabled属性来禁用开关,禁用状态下开关不可点击

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

加载状态

通过loading属性设置开关为加载状态,加载状态下开关不可点击

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

自定义大小

通过size属性自定义开关的大小

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

自定义颜色

active-color属性表示打开时的背景色,inactive-color表示关闭时的背景色

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

异步控制

需要异步控制开关时,可以使用value属性和input事件代替v-model,并在input事件回调函数中手动处理开关状态

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

搭配单元格使用

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

API

Props

参数 说明 类型 默认值
v-model 开关选中状态 any false
loading 是否为加载状态 boolean false
disabled 是否为禁用状态 boolean false
size v2.2.11 开关尺寸,默认单位为px number | string 30px
active-color 打开时的背景色 string #1989fa
inactive-color 关闭时的背景色 string white
active-value 打开时对应的值 any true
inactive-value 关闭时对应的值 any false

Events

事件名 说明 回调参数
change 开关状态切换时触发 value: any
click v2.2.11 点击时触发 event: Event