I have carefully read and re-read the Vue docs "Reactivity in Depth" and the API for vm.$set and Vue.set but I am still having a difficult time determining when to use which. It is important for me to be able to distinguish between the two because in my current Laravel project, we are setting a lot of properties on objects dynamically.
文档中的区别似乎在于虚拟机使用的语言$设置为"适用于Vue实例",而Vue.设置为"用于普通数据对象"和Vue.集合是全局的:
然而,有一些方法可以添加一个属性,并使其在之后具有react 性
对于Vue实例,可以使用$set(路径、值)实例方法:
vm.$set('b', 2)
// `vm.b` and `data.b` are now reactive
对于普通数据对象,可以使用全局Vue.设置(对象、键、,
Vue.set(data, 'c', 3)
// `vm.c` and `data.c` are now reactive
最后,我想知道是否可以将执行上述操作的第三个"选项"(即一次添加多个属性)用作上述两个选项中任何一个的等效替代品(只添加一个属性而不是多个)?
Sometimes you may want to assign a number of properties on to an existing object, for example using Object.assign() or _.extend(). However, new properties added to the object will not trigger changes. In such cases, create a fresh object with properties from both the original object and the mixin object:
// instead of `Object.assign(this.someObject, { a: 1, b: 2 })`
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })