vue中$set的用法 $set用来更新数组或对象 $set接收3个参数:参数1:参数是需要更新的数组或对象, 参数2:是数组的下标或者对象的属性名, 参数3:是更新的内容
$set用于更新数组:
let arr =["周一","周二","周三","周四","周五","周六"]; //第一个参数是数组,第二个参数是下标索引,第三个参数是新的内容 this.$set(arr,5,"周日"]; //结果 console.log(arr);//arr=["周一","周二","周三","周四","周五","周日"]
$set用于更新对象:
//对象的属性sex的值更新为"女" let obj={name:"小明",age:18,sex:"男"}; //第一个参数是对象,第二个参数是更新的属性名称,第三个参数是更新的内容 this.$set(obj,"sex","女"); //结果 console.log(obj)//obj={name:"小明",age:18,sex:"女"}