How to access variable in "setup" in a closure that has a parameter with the same name? #9917
-
This component is a share modal that can be invoked by other components. so I use exposed function to pass data instead of props. I wonder if there is a way to access variable in "setup" that has the same name with parameter. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The issue with the original code lies in the attempt to use In your case, In the <script setup>
import { ref } from "vue"
const apple = ref()
const modal = ref()
defineExpose({
open: (newApple) => {
apple.value = newApple
modal.value.open()
}
})
</script> Now, you can access the |
Beta Was this translation helpful? Give feedback.
The issue with the original code lies in the attempt to use
this
within thedefineExpose
function. In Vue 3, when using the Composition API with<script setup>
, you don't have access tothis
inside the script. Instead, you directly reference the reactive variables created withref
.In your case,
apple
andmodal
are both refs, so you should useapple.value
andmodal.value
to access and modify their values. The corrected code reflects this adjustment, eliminating the attempt to usethis
within thedefineExpose
function.In the
defineExpose
function, you can directly reference theapple
ref without usingthis
. Here's the corrected code: