vue中set强制页面刷新数据 设置对象和数组的方式

第一种:Vue.set( )

对象:

语法:Vue.set(obj,key,value);

obj: {
    active: false
};
Vue.set(this.obj,"active",true);
数组:

语法:Vue.set(list,index,value);

list: [0,1,2,3];
Vue.set(this.list,0,10);
数组里的对象:

语法:Vue.set(list,index,{...list,key:value});

list: [{
    id: 0,
    active: false
}, {
    id: 1,
    active: false
}];
Vue.set(this.list,0,{ ...list[0], active: true });

第二种:this.$set( )

对象:

语法:this.$set(obj,key,value);

obj: {
    active: false
};
this.$set(this.obj,"active",true);
数组:

语法:this.$set(list,index,value);

list: [0,1,2,3];
this.$set(this.list,0,10);
数组里的对象:

语法:this.$set(list,index,{...list,key:value});

list: [{
    id: 0,
    active: false
}, {
    id: 1,
    active: false
}];
this.$set(this.list,0,{ ...list[0], active: true });

你可能感兴趣的:(vue.js,前端,javascript)