I carefully read and re-read Vue docs "Reactivity in depth" and the API for vm. $ set and Vue.set , but itβs still hard for me to determine which time to use. It is important for me to distinguish between the two, because in my current Laravel project we set many properties on objects dynamically.
The difference in the documents seems to be between the language in which vm. $ set is "For an instance of Vue", and Vue.set is "For simple 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 instance method $ set (path, value):
vm.$set('b', 2) // `vm.b` and `data.b` are now reactive
For simple data objects, you can use the global Vue.set (object, key, value):
Vue.set(data, 'c', 3)
Finally, I was wondering if the third βoptionβ can be made higher (which consists of adding several properties at a time) can be used as an equivalent replacement for one of the two options above (adding instead only 1 property of several)?
Sometimes you may need to assign a number of properties to an existing object, for example, using Object.assign () or _.extend (). However, new properties added to the object will not cause changes. In such cases, create a new 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 })