Subscribe on changes!

defineModel is counterintuitive

avatar
Dec 6th 2023

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);
  })
}
avatar
Dec 6th 2023

As Alfred stated

avatar
Dec 6th 2023

@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.