W
W
WebDev2019-02-01 16:54:34
Vue.js
WebDev, 2019-02-01 16:54:34

How to correctly assign a value to a property in vue?

Somewhere in the documentation it was said that the nested properties of an object cannot be directly assigned values, since vue will not be able to track their change.
For example, you can't do this: Instead, you need to use Vue.set: For some reason, the first option works for me. How right?
product.info.name = 'New value';
Vue.set(product.info, 'name', 'New value');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2019-02-01
@kirill-93

When creating an instance, Vue goes through all the available properties to the full depth and attaches getters and setters to them, which trigger internal mechanisms for reacting to property changes, making them reactive.
When you simply add a new property, no getters/setters are automatically attached to it, and Vue cannot track changes.
That is why to add new properties, you must use the Vue.set () method, which, when created through defineProperty, will initialize all internal reactivity mechanisms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question