Switch 开关

开关选择器。

何时使用

  • 需要表示开关状态/两种状态之间的切换时;
  • checkbox的区别是,切换 switch 会直接触发状态改变,而 checkbox 一般用于状态标记,需要和提交操作配合。

代码演示

简单

最简单的用法。

  1. <template>
  2. <v-switch v-model="checked"></v-switch>
  3. </template>
  4. <script>
  5. export default {
  6. data: ()=> ({
  7. checked: true
  8. })
  9. };
  10. </script>

不可用

Switch 失效状态。

  1. <template>
  2. <v-switch :disabled="disabled"></v-switch>
  3. <br><br>
  4. <v-button type="primary" @click="_toogle">Toggle disabled</v-button>
  5. </template>
  6. <script>
  7. export default {
  8. data: ()=> ({
  9. disabled: false
  10. }),
  11. methods: {
  12. _toogle() {
  13. this.disabled = !this.disabled
  14. }
  15. }
  16. };
  17. </script>

文字和图标

带有文字和图标。

  1. <v-switch>
  2. <span slot="checkedChildren"></span>
  3. <span slot="unCheckedChildren"></span>
  4. </v-switch>
  5. <br><br>
  6. <v-switch>
  7. <span slot="checkedChildren">
  8. <i class="anticon anticon-check"></i>
  9. </span>
  10. <span slot="unCheckedChildren">
  11. <i class="anticon anticon-cross"></i>
  12. </span>
  13. </v-switch>

两种大小

size='small' 表示小号开关

  1. <v-switch></v-switch>
  2. <br>
  3. <v-switch size="small"></v-switch>

自定义状态值

属性名称:true-value false-value以下例子定义:选中时为 1 ,未选中时为 0组件当前状态为选中

  1. <template>
  2. <v-switch v-model="checkStatus" :true-value="1" :false-value="0"></v-switch>
  3. <v-button @click="getStatus" size="small" type="primary">获取状态码</v-button>
  4. </template>
  5. <script>
  6. export default {
  7. data: ()=> ({
  8. checkStatus: 1
  9. }),
  10. methods: {
  11. getStatus(){
  12. this.$message.info("当前状态码为:" + this.checkStatus);
  13. }
  14. }
  15. };
  16. </script>

API

Switch Props

属性说明类型默认值
value指定当前是否选中Booleanfalse
disabeled指定当前是否被禁用Booleanfalse
size开关大小("default" or "small")Stringdefault
slot:checkedChildren选中时的内容slot node-
slot:unCheckedChildren非选中时的内容slot node-
true-value选中时自定义值Any-
false-value未选中时自定义值Any-