# Switch 开关 ### 引入 在`app.json`或`index.json`中引入组件,默认为`ES6`版本,`ES5`引入方式参见[快速上手](#/quickstart) ```json "usingComponents": { "van-switch": "path/to/vant-weapp/dist/switch/index" } ``` ## 代码演示 ### 基础用法 ```html ``` ```javascript Page({ data: { checked: true }, onChange({ detail }) { // 需要手动对 checked 状态进行更新 this.setData({ checked: detail }); } }); ``` ### 禁用状态 ```html ``` ### 加载状态 ```html ``` ### 自定义大小 ```html ``` ### 自定义颜色 ```html ``` ### 异步控制 ```html ``` ```js Page({ data: { checked: true }, onChange({ detail }) { wx.showModal({ title: '提示', content: '是否切换开关?', success: res => { if (res.confirm) { this.setData({ checked2: detail }); } } }); } }); ``` ### Props | 参数 | 说明 | 类型 | 默认值 | 版本 | |-----------|-----------|-----------|-------------|-------------| | name | 在表单内提交时的标识符 | *string* | - | - | | checked | 开关选中状态 | *any* | `false` | - | | loading | 是否为加载状态 | *boolean* | `false` | - | | disabled | 是否为禁用状态 | *boolean* | `false` | - | | size | 开关尺寸 | *string* | `30px` | - | | active-color | 打开时的背景色 | *string* | `#1989fa` | - | | inactive-color | 关闭时的背景色 | *string* | `#fff` | - | | active-value | 打开时的值 | *any* | `true` | - | | inactive-value | 关闭时的值 | *any* | `false` | - | ### Events | 事件名 | 说明 | 参数 | |-----------|-----------|-----------| | bind:change | 开关状态切换回调 | event.detail: 是否选中开关 | ### 外部样式类 | 类名 | 说明 | |-----------|-----------| | custom-class | 根节点样式类 | | node-class | 圆点样式类 |