element-plus 中的Switch开关 注意点
在使用Switch开关时,发现v-model可以绑定boolean / string / number 三种类型的数据,默认绑定的是布尔型。但是后台返回的数据是数字形型的,0或1
const abc = "1"; <el-switch v-model="abc" active-value="1" inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
这样展示是正常的,因为 active-value="1" 这里的选中的值1是字符串型的,上面定义的abc也是字符串型的。
如果我们abc定义为number:
const abc = 1; <el-switch v-model="abc" :active-value="1" :inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
switch中选中的active-value前面必须加上冒号 不然是字符串型,和abc的number不相等。