Answer the question
In order to leave comments, you need to log in
Why can't I get the old value of an observable in Vue?
Listening through the watch object, I need to know the old value of one of the object's properties, but I only get the new properties. Why is that?
watch: {
worker: {
handler: function(newWorker, oldWorker) {
alert (newWorker.sum + ' ' + oldWorker.sum);
},
deep: true
}
}
Answer the question
In order to leave comments, you need to log in
We open the documentation , we read:
Note that when modifying (rather than replacing) an object or array, the old and new values will be the same when the callback is called, since they refer to the same object or array. Vue does not save copies of the object before the change.
watch: {
'obj.propName'(newValue, oldValue) {
...
},
},
obj.propName = value
, but it will be . Well, then you can already compare the properties in the old and new objects to find which one was specifically changed. obj = { ...obj, propName: value }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question