Closed hfyZone closed 2 weeks ago
Regarding 1) You can't replace reference to reactive state like that, it's explained here https://vuejs.org/guide/essentials/reactivity-fundamentals.html#limitations-of-reactive If you want to update reactive state with a new object, use Object.assign(student, newState)
. Other solution is using ref. The choice is yours which one you want to use
Regarding 2) it works as expected. The changeToBrain()
function will always trigger the callback because you're assigning a new object. In changeSalary(),
you only update the salary property, but the tracked object itself hasn't changed. That's why the watcher callback is not triggered unless you explicitly enable deep property tracking.
As @sqal said.
Vue version
3.5.12
Link to minimal reproduction
https://github.com/hfyZone/vue-playground
Steps to reproduce
nom run dev
.{deep:true}
to line 18th in the fileTeacher.vue
,Click button "change Salary Lois" and button "Change to Brain".What is expected?
What is actually happening?
Two bugs of vue@3.5.12
1 Vue fails to deal with a ref Object when watch triggered by the change of the reference of this ref Object
In Student.vue, if you click button 'Change To Stewie',then vue will fail to deal with this ref Object. The button 'Change To Stewie' just change the reference of the 'student' to a new Object
{name:"Stewie", salary: 0}
.2 whether is deep traversal of the source forced if it is an object?
According to the document:
But in Teacher.vue, an object 'teacher' is passed to watch,
Only if {deep:true} is added, then changeSalary() will trigger watch;
Whether {deep:true} is added or not, changeToBrain() always triggers watch;
Firstly,when {deep:true} is not added, a change of a property or the entire Object leads to different performance of watch, the first one doesn`t trigger watch, the second one triggers watch;
Secondly,the document tells that 'force deep traversal of the source if it is an object',but you have to add {deep:true}, I dont know if this is a problem of the code or the document.
System Info
No response
Any additional comments?
Actually I am just learning vue and code accordingly to the document.I have no idea about whether the two usage above is best practice or not, Its just Contradictory with the document and nothing alike is in stack overflow, so I report the two problems.