Switch 开关

表示两种相互对立的状态间的切换,多用于触发「开/关」。

基本用法

绑定v-model到一个Boolean类型的变量。可以使用active-color属性与inactive-color属性来设置开关的背景色。

 1 <el-switch
 2   v-model="value2"
 3   active-color="#13ce66"
 4   inactive-color="#ff4949">
 5 </el-switch>
 6 
 7 <script>
 8   export default {
 9     data() {
10       return {
11         value1: true,
12         value2: true
13       }
14     }
15   };
16 </script>
View Code

 

文字描述

使用active-text属性与inactive-text属性来设置开关的文字描述。

<el-switch
  v-model="value3"
  active-text="按月付费"
  inactive-text="按年付费">
</el-switch>
<el-switch
  style="display: block"
  v-model="value4"
  active-color="#13ce66"
  inactive-color="#ff4949"
  active-text="按月付费"
  inactive-text="按年付费">
</el-switch>

<script>
  export default {
    data() {
      return {
        value3: true,
        value4: true
      }
    }
  };
</script>
View Code

 

扩展的 value 类型

设置active-valueinactive-value属性,接受BooleanStringNumber类型的值。

 1 <el-tooltip :content="'Switch value: ' + value5" placement="top">
 2   <el-switch
 3     v-model="value5"
 4     active-color="#13ce66"
 5     inactive-color="#ff4949"
 6     active-value="100"
 7     inactive-value="0">
 8   </el-switch>
 9 </el-tooltip>
10 
11 <script>
12   export default {
13     data() {
14       return {
15         value5: '100'
16       }
17     }
18   };
19 </script>
View Code

 

禁用状态

设置disabled属性,接受一个Boolean,设置true即可禁用。

 1 <el-switch
 2   v-model="value6"
 3   disabled>
 4 </el-switch>
 5 <el-switch
 6   v-model="value7"
 7   disabled>
 8 </el-switch>
 9 <script>
10   export default {
11     data() {
12       return {
13         value6: true,
14         value7: false
15       }
16     }
17   };
18 </script>
View Code

 

Attributes

参数说明类型可选值默认值
disabled 是否禁用 boolean false
width switch 的宽度(像素) number 40
active-icon-class switch 打开时所显示图标的类名,设置此项会忽略 active-text string
inactive-icon-class switch 关闭时所显示图标的类名,设置此项会忽略 inactive-text string
active-text switch 打开时的文字描述 string
inactive-text switch 关闭时的文字描述 string
active-value switch 打开时的值 boolean / string / number true
inactive-value switch 关闭时的值 boolean / string / number false
active-color switch 打开时的背景色 string #409EFF
inactive-color switch 关闭时的背景色 string #C0CCDA
name switch 对应的 name 属性 string

Events

事件名称说明回调参数
change switch 状态发生变化时的回调函数 新状态的值

Methods

方法名说明参数
focus 使 Switch 获取焦点 -
posted @ 2018-03-13 06:04  大姐姐小姐姐  阅读(2764)  评论(0编辑  收藏  举报