工作记录
背景:el-switch绑定值的问题
第一种:
v-model绑定布尔值
<template>
<el-switch
v-model="value"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
<script>
export default {
name: "home",
data() {
return {
//页面加载初始化为开启状态
value:true
}
}
}
</script>
<style scoped>
</style>
第二种:
v-model绑定布尔值,当绑定的值为 active-value或inactive-value的相等值时,开启或关闭
①情况一:
当active-value="1"或active-value=1(或inactive-value=“0"或inactive-value=0)时,表示v-model绑定的值须为’1’或"0”——(值为string类型),否则虽然也能切换(el-switch的属性),并没有和v-model绑定的值有关系;
<template>
<el-switch
v-model="value"
active-value="1"
inactive-value="0"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
<script>
export default {
name: "home",
data() {
return {
//value需为字符串
value: "1"
}
}
}
</script>
<style scoped>
</style>
②情况二:
当 :active-value="1"或 :active-value=1 (或:inactive-value="0"或 :inactive-value=0)时,表示v-model绑定的值须为1或0——(值为Number类型),否则虽然也能切换(el-switch的属性),并没有和v-model绑定的值有关系;
<template>
<el-switch
v-model="value"
:active-value="1"
:inactive-value="0"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
<script>
export default {
name: "home",
data() {
return {
//value需为Number类型
value: 1
}
}
}
</script>
<style scoped>
</style>
change事件
当数据改变时,调用change绑定的方法。该回调函数有一个参数,为当前状态的value值
<template>
<div>
<el-switch
v-model="value"
active-value="1"
inactive-value="0"
@change='changeStatus'
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</div>
</template>
<script>
export default {
name: "home",
data() {
return {
value:"1"
}
},
methods: {
//该参数为当前的value值
changeStatus: function (callback) {
console.log(callback);
}
}
}
</script>
<style scoped>
</style>