Description
Vue version
3.5.13
Link to minimal reproduction
Steps to reproduce
This baffled me for the last few hours. If a defineModel is assigned a value, that value cannot subsequently be read. This behavior differs from refs, which can be read as soon as they're written, and my basic understanding is that defineModel
defines a type of ref? It also seems to only happen when an undefined value is assigned to the model, which then I guess gets updated and somehow interrupts value retrieval for the model? 🤷♂
What is expected?
Look at this component:
<script setup>
import { ref } from 'vue'
const startDate = defineModel('startDate')
const endDate = defineModel('endDate')
let start = ref()
let end = ref()
if (!startDate.value) {
startDate.value = 'Jan 1'
endDate.value = 'Feb 1'
start.value = startDate.value
end.value = endDate.value
}
</script>
<template>
<div>
Start is {{ start }}<br>
End is {{ end }}
</div>
</template>
Ask any developer what will be output. If they say the output will be this, they are wrong:
Start is Jan 1
End is Feb 1
The output is actually:
Start is
End is Feb 1
What is actually happening?
defineModel
has wildly different and unpredictable behavior based on whether or not refs are assigned.
System Info
System:
OS: macOS 15.1.1
CPU: (10) arm64 Apple M1 Pro
Memory: 89.83 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.12.1 - ~/Library/Caches/fnm_multishells/78455_1738117758436/bin/node
Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/66567_1738082123979/bin/yarn
npm: 10.5.0 - ~/Library/Caches/fnm_multishells/78455_1738117758436/bin/npm
pnpm: 8.10.2 - /opt/homebrew/bin/pnpm
Browsers:
Chrome: 132.0.6834.111
Safari: 18.1.1
npmPackages:
vue: ~3.5.12 => 3.5.13
Any additional comments?
By the way, the reason I'm doing it this way vs a default value in defineModel
is that the value is set by other types of reactive values. This is just a simple repro.