-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defineModel assigned a value cannot be read #12787
Comments
https://vue-land.github.io/faq/emit-synchronous |
So, I just read that thread and the fact it was listed as "expected behavior" is BONKERS. I updated my example above with the following example: 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
} How does it make any reasonable sense, that based on this code, |
It only seems bonkers if you think of it as a ref and expect it to act like a ref instead of as what it actually is, reading the value from a prop and emitting events when you write to the model. If it is too complicated to remember how |
@matthew-dean if you directly assign the model to <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 <-- instead of accessing .value you can directly assign it to the model
end.value = endDate <-- same here
}
</script>
<template>
<div>
Start is {{ start }}<br>
End is {{ end }}
</div>
</template> |
Vue version
3.5.13
Link to minimal reproduction
https://play.vuejs.org/#eNqVUl1L7DAQ/Stz89IVvC2y974s9cJVV1DwA/UxILWdrtE0LUlaF0r/u5O0m9X1A3xL5pw5OSczPfvfNHHXIluw1ORaNBYM2rb5x5Womlpb6EFjCQOUuq4gImoUoOO6aqZ6nLiLUyI4r5UhHZtpe5JZhEMnMdvjKk3GN0idLharRhJON4DUi3W/q7pAuQi9h5yFM2eQEDdN3jSyfWYNvVeKVfxkakU5eifHWU56QqK+aqwgP5wtwCMOy6SsX859zeoW9zf1/BHz50/qT2btapxdazSoO7ISMHK3QjvCy9tLXNM5gJSmlcT+BrxBU8vWeRxpR60qyPYbnnd75r9cqNWdWa4tKrMJ5Yw65uD5nNEI3Fd+FX1rdx7/8X1cDfSLm/H9bBE+TrrAUii8cFOcRQGIaPgjF1XxGXMqOx5XEifN7ea4EnG2BVHC7FfQj7tMtrg3xtypUk90nik4IL+weX8LneLDBPm+AOyoTL0BfqdDX/j9chei8weAW59LGOj7KeMwpA96QpcUccRc2IFUqTkZu3f3/r5D7baAJjaP/8YHcza8AtXQR/o=
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:
Ask any developer what will be output. If they say the output will be this, they are wrong:
The output is actually:
What is actually happening?
defineModel
has wildly different and unpredictable behavior based on whether or not refs are assigned.System Info
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.The text was updated successfully, but these errors were encountered: