Inconsistent component ref when using `defineExpose`
Vue version
3.3.4
Link to minimal reproduction
Steps to reproduce
The exposed vue instance when adding a ref to a Vue component in Script Setup differs if you have defined exposes with defineExpose()
.
Create two components, which take a single prop, identical except that one has a defineExpose
.
Import these components in a parent component, and set a ref on them. Then log that ref's value.myProp (in a watcher, or onmounted).
What is expected?
The two logs log the same value.
What is actually happening?
If you do not have a defineExpose
in your component, a parent component can access the props sent to that component via myRef.value.myProp
, but as soon as you define exposes in the component, myRef.value.myProp
returns undefined
.
System Info
No response
Any additional comments?
The documentation for defineExpose
(https://vuejs.org/api/sfc-script-setup.html#defineexpose) doesn't mention anything about that using it change the behaviour of other properties on the ref. So either it seems the documentation is unclear and needs to be clarified, or it is indeed a bug.
defineExpose
in the setup script is macro syntax, so it will compile to the expose
call in the setup function.
In the setup function docs, we will see that the expose function call will "close" the current instance, exposing only the incoming parameters for external access .
Without defineExpose
, it's as if nothing is returned in the setup function
Right, thanks for the explanation. I still however believe that the docs are lacking in this case.
From the docs for <script setup>
it says that these are "closed by default", which doesn't seem to be the case? As per my reproduction link. I take this to mean the same as calling expose();
in the setup()
function.
you're right in that script setup
components are designed to always to be closed. And they are in DEV mode (switch the playground from PROD
to DEV
(button in the upper right) and it works as you expect it to.
It seems that components compiled for prod do not generate the expose proxy, which seems like a bug to me.