2020-08-13 15:18:26 +08:00
## Switch
Switch es utilizado para realizar cambios entre dos estados opuestos.
### Uso básico
:::demo Enlace `v-model` a una variable de tipo `Boolean` . Los atributos `active-color` y `inactive-color` deciden el color de fondo en cada estado.
```html
2021-04-26 11:53:09 +08:00
< el-switch v-model = "value1" > < / el-switch >
< el-switch v-model = "value2" active-color = "#13ce66" inactive-color = "#ff4949" >
2020-08-13 15:18:26 +08:00
< / el-switch >
< script >
export default {
data() {
return {
value1: true,
2021-04-26 11:53:09 +08:00
value2: true,
2020-08-13 15:18:26 +08:00
}
2021-04-26 11:53:09 +08:00
},
}
2020-08-13 15:18:26 +08:00
< / script >
```
2021-04-26 11:53:09 +08:00
2020-08-13 15:18:26 +08:00
:::
### Texto de descripción
2021-04-26 11:53:09 +08:00
2020-08-13 15:18:26 +08:00
:::demo Puede agregar los atributos `active-text` y `inactive-text` para mostrar los textos.
```html
< el-switch
v-model="value1"
active-text="Pay by month"
2021-04-26 11:53:09 +08:00
inactive-text="Pay by year"
>
2020-08-13 15:18:26 +08:00
< / el-switch >
< el-switch
style="display: block"
v-model="value2"
active-color="#13ce66"
inactive-color="#ff4949"
active-text="Pay by month"
2021-04-26 11:53:09 +08:00
inactive-text="Pay by year"
>
2020-08-13 15:18:26 +08:00
< / el-switch >
< script >
export default {
data() {
return {
value1: true,
2021-04-26 11:53:09 +08:00
value2: true,
2020-08-13 15:18:26 +08:00
}
2021-04-26 11:53:09 +08:00
},
}
2020-08-13 15:18:26 +08:00
< / script >
```
2021-04-26 11:53:09 +08:00
2020-08-13 15:18:26 +08:00
:::
### Tipos de valores extendidos
:::demo Puede establecer los atributos `active-value` y `inactive-value` . Ambos reciben valores de tipo `Boolean` , `String` o `Number` .
```html
< el-tooltip :content = "'Switch value: ' + value" placement = "top" >
< el-switch
v-model="value"
active-color="#13ce66"
inactive-color="#ff4949"
active-value="100"
2021-04-26 11:53:09 +08:00
inactive-value="0"
>
2020-08-13 15:18:26 +08:00
< / el-switch >
< / el-tooltip >
< script >
export default {
data() {
return {
2021-04-26 11:53:09 +08:00
value: '100',
2020-08-13 15:18:26 +08:00
}
2021-04-26 11:53:09 +08:00
},
}
2020-08-13 15:18:26 +08:00
< / script >
```
:::
### Disabled
:::demo Agregar el atributo `disabled` desactiva el componente Switch.
```html
2021-04-26 11:53:09 +08:00
< el-switch v-model = "value1" disabled > < / el-switch >
< el-switch v-model = "value2" disabled > < / el-switch >
2020-08-13 15:18:26 +08:00
< script >
export default {
data() {
return {
value1: true,
2021-04-26 11:53:09 +08:00
value2: false,
2020-08-13 15:18:26 +08:00
}
2021-04-26 11:53:09 +08:00
},
}
2020-08-13 15:18:26 +08:00
< / script >
```
2021-04-26 11:53:09 +08:00
2020-08-13 15:18:26 +08:00
:::
2020-11-11 18:19:52 +08:00
### Loading
:::demo Setting the `loading` attribute to `true` indicates a loading state on the Switch.
```html
2021-04-26 11:53:09 +08:00
< el-switch v-model = "value1" loading > < / el-switch >
< el-switch v-model = "value2" loading > < / el-switch >
2020-11-11 18:19:52 +08:00
< script >
export default {
data() {
return {
value1: true,
2021-04-26 11:53:09 +08:00
value2: false,
}
},
}
< / script >
```
:::
### prevent switching
2021-09-12 18:02:52 +08:00
:::demo set the `before-change` property, If `false` is returned or a `Promise` is returned and then is rejected, will stop switching.
2021-04-26 11:53:09 +08:00
```html
2021-09-12 18:02:52 +08:00
< el-switch v-model = "value1" :loading = "loading1" :before-change = "beforeChange1" >
2021-04-26 11:53:09 +08:00
< / el-switch >
2021-09-12 18:02:52 +08:00
< el-switch v-model = "value2" :loading = "loading2" :before-change = "beforeChange2" >
2021-04-26 11:53:09 +08:00
< / el-switch >
< script >
import { reactive, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
export default {
setup() {
const status1 = reactive({
value1: false,
loading1: false,
})
const beforeChange1 = () => {
status1.loading1 = true
2021-09-04 19:29:28 +08:00
return new Promise((resolve) => {
2021-04-26 11:53:09 +08:00
setTimeout(() => {
status1.loading1 = false
ElMessage.success('switch success')
return resolve(true)
}, 1000)
})
}
const status2 = reactive({
value2: false,
loading2: false,
})
const beforeChange2 = () => {
status2.loading2 = true
return new Promise((resolve, reject) => {
setTimeout(() => {
status2.loading2 = false
ElMessage.error('switch failed')
return reject(new Error('error'))
}, 1000)
})
}
return {
...toRefs(status1),
...toRefs(status2),
beforeChange1,
beforeChange2,
2020-11-11 18:19:52 +08:00
}
2021-04-26 11:53:09 +08:00
},
}
2020-11-11 18:19:52 +08:00
< / script >
```
2021-04-26 11:53:09 +08:00
2020-11-11 18:19:52 +08:00
:::
2020-08-13 15:18:26 +08:00
### Atributos
2021-09-04 19:29:28 +08:00
| Atributo | Descripción | Tipo | Valores aceptados | Por defecto |
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ----------------- | ----------- |
| model-value / v-model | valor vinculante, debe ser equivalente al `active-value` o al `inactive-value` . El tipo por defecto es el tipo `boolean` . | boolean / string / number | — | — |
| disabled | si Switch esta deshabilitado | boolean | — | false |
| loading | whether Switch is in loading state | boolean | — | false |
| width | ancho del componente Switch | number | — | 40 |
| active-icon-class | nombre de la clase del icono mostrado en el estado `on` , sobrescribe `active-text` | string | — | — |
| inactive-icon-class | nombre de la clase del icono mostrado en el estado `off` , sobrescribe `inactive-text` | string | — | — |
| active-text | texto mostrado en el estado `on` | string | — | — |
| inactive-text | texto mostrado en el estado `off` | string | — | — |
| active-value | cambia su valor cuando se encuentra en el estado `on` | boolean / string / number | — | true |
| inactive-value | cambia su valor cuando se encuentra en el estado `off` | boolean / string / number | — | false |
| active-color | color de fondo cuando se encuentra en el estado `on` | string | — | #409EFF |
| inactive-color | color de fondo cuando se encuentra en el estado `off` | string | — | #C0CCDA |
| border-color | color del borde del switch. | string | — | — |
| name | nombre de entrada del componente Switch | string | — | — |
| validate-event | si se debe lanzar la validación de formulario | boolean | — | true |
| before-change | before-change hook before the switch state changes. If `false` is returned or a `Promise` is returned and then is rejected, will stop switching | function | — | — |
2020-08-13 15:18:26 +08:00
### Eventos
| Nombre del evento | Descripción | Parametro |
| ----------------- | --------------------------------- | --------- |
| change | se dispara cuando el valor cambia | valor |
2021-04-26 11:53:09 +08:00
2020-08-13 15:18:26 +08:00
después de cambiar
### Metodos
2021-04-26 11:53:09 +08:00
2020-08-13 15:18:26 +08:00
| Metodo | Descripción | Parametro |
| ------ | ------------------------- | --------- |
| focus | foco al componente Switch | — |