defineModel is counterintuitive
Vue version
3.3.8
Link to minimal reproduction
Steps to reproduce
- click gray block times
- check console.log
- 'previous' and 'after' are always same, it is counterintuitive
What is expected?
previous: false
after: true
or
previous: true
after: false
What is actually happening?
System Info
No response
Any additional comments?
No response
This behavior is expected. Within defineModel
, the value of modelValue
is changed through event notifications, and it is modified by the parent component. When it changes, it triggers the update of the component. Since component updates are asynchronous, you must wait until the component is updated to obtain the latest value. You can use nextTick
to retrieve the most up-to-date value.
function onClick() {
console.log('previous', modelValue.value);
modelValue.value = !modelValue.value;
nextTick(() => {
console.log('after', modelValue.value);
})
}
@Alfred-Skyblue
Thank you for response in minutes.
So modelValue.value = xxx
is equal to emit('update:value', xxx)
with its setter
essentially, right?
But the code, from JS side, it looks not the same way, it is counterintuitive as mentioned in title.
It likes MagicCode.
I understand that it works as design.
Thanks again.