-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Description
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.
Activity
linghaoSu commentedon Sep 5, 2023
defineExpose
in the setup script is macro syntax, so it will compile to theexpose
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 functionfull demo with setup function
perenstrom commentedon Sep 5, 2023
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 callingexpose();
in thesetup()
function.LinusBorg commentedon Sep 6, 2023
you're right in that
script setup
components are designed to always to be closed. And they are in DEV mode (switch the playground fromPROD
toDEV
(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.
expose
removesinlineTemplate
restriction #9159perenstrom commentedon Sep 7, 2023
Oh, right! That makes sense then why we encountered it. We encountered it in dev mode (in project A) since the anomaly occurred in our built and packaged internal component library (in project B)! So an edge case for sure. Swift fix by @baiwusanyu-c !