Switch 开关
Switch 开关
表示两种相互对立的状态间的切换,多用于触发「开/关」。
基本用法
绑定v-model到一个Boolean类型的变量。可以使用active-color属性与inactive-color属性来设置开关的背景色。
<el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949"></el-switch><script> export default { data() { return { value: true } } };</script>
文字描述
使用active-text属性与inactive-text属性来设置开关的文字描述。
<el-switch v-model="value1" active-text="按月付费" inactive-text="按年付费"></el-switch><el-switch style="display: block" v-model="value2" active-color="#13ce66" inactive-color="#ff4949" active-text="按月付费" inactive-text="按年付费"></el-switch><script> export default { data() { return { value1: true, value2: true } } };</script>
扩展的 value 类型
设置active-value和inactive-value属性,接受Boolean, String或Number类型的值。
<el-tooltip :content="'Switch value: ' + value" placement="top"> <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949" active-value="100" inactive-value="0"> </el-switch></el-tooltip><script> export default { data() { return { value: '100' } } };</script>
禁用状态
设置disabled属性,接受一个Boolean,设置true即可禁用。
<el-switch v-model="value1" disabled></el-switch><el-switch v-model="value2" disabled></el-switch><script> export default { data() { return { value1: true, value2: false } } };</script>
Attributes
| value / v-model |
绑定值 |
boolean / string / number |
— |
— |
|
|
|
|
|
| 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 |
— |
— |
|
|
|
|
|
| validate-event |
改变 switch 状态时是否触发表单的校验 |
boolean |
- |
true |
|
|
|
|
|
Events
| change |
switch 状态发生变化时的回调函数 |
新状态的值 |
|
|
|
Methods