vue-property-decorator icon indicating copy to clipboard operation
vue-property-decorator copied to clipboard

oldVal equals to newVal in Watch decorator

Open eakarpov opened this issue 4 years ago • 5 comments

Issue #140 is not fixed but is closed!

I change my model to the new val, it triggers the watch listener, but both new and old values equal to the new value (the result of the change).

Is it an expected behavior? Thank you.

eakarpov avatar Sep 02 '19 15:09 eakarpov

When mutating (rather than replacing) an Object or an Array, the old value will be the same as new value because they reference the same Object/Array. Vue doesn’t keep a copy of the pre-mutate value (https://vuejs.org/v2/api/#vm-watch)

Djaler avatar Oct 06 '19 17:10 Djaler

@Djaler And how should we check what property the object has changed when mutated?

eakarpov avatar Oct 09 '19 09:10 eakarpov

You can make some hacks. For example, by keeping deep clone of object and comparing new value with this copy (of previous value). But I recommend always return new object rather than mutate the same

Djaler avatar Oct 09 '19 09:10 Djaler

Okay, thanks

eakarpov avatar Oct 09 '19 10:10 eakarpov

/close

sylann avatar Aug 25 '21 12:08 sylann