Subscribe on changes!

`old value` in parameters of `watch` callback maybe incorrect

avatar
Sep 12th 2023

Vue version

3.3.4

Link to minimal reproduction

Vue SFC Playground link

Steps to reproduce

  1. open SFC Playground links above
  2. open console
  3. click buttons in "oldVal incorrect" area, and check console, two messages will be printed on each click, and they will be the same, which is unexpected.

https://github.com/vuejs/core/assets/51878637/c1fe1621-7586-48a1-9984-6d12441ea6f8

What is expected?

Printed second parameter oldVal of the watch callback should not be equal to the first parameter val, since it's internal value has been changed when user click button.

What is actually happening?

The old value is the same as the latest value, exactly like when clicking buttons in "oldVal correct" area.

System Info

No response

Any additional comments?

No response

avatar
Sep 12th 2023

It is expected that the watch newVal and oldVal of the object are the same, because the Vue object type saves a reference, not a value. When you modify the reference of the object, you can get different newVal and oldVal. When you modify the object properties, The obtained newVal and oldVal are the same because both values point to the same object.

avatar
Sep 12th 2023

Oh thanks for pointing out! 🫶

It's a little counterintuitive though so that I forgot I read this part of document before and should've known that 🥲