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.
The distinction in the docs seems to be between the language that vm.$set is "For Vue instance" while Vue.set is "For plain data objects" and that Vue.set is global:
However, there are ways to add a property and make it reactive after
an instance has been created.
For Vue instances, you can use the $set(path, value) instance method:
vm.$set('b', 2)
// `vm.b` and `data.b` are now reactive
For plain data objects, you can use the global Vue.set(object, key,
value) method:
Vue.set(data, 'c', 3)
// `vm.c` and `data.c` are now reactive
Finally, I was wondering if the 3rd "option" of doing the above (which is for adding multiple properties at one time) could be used as an equivalent substitute for either of the 2 options above (by adding just 1 property instead of multiple)?
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 })
question from:
https://stackoverflow.com/questions/36671106/what-is-the-difference-between-vm-set-and-vue-set 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…