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
thiswithin thedefineExposefunction. In Vue 3, when using the Composition API with<script setup>, you don't have access tothisinside the script. Instead, you directly reference the reactive variables created withref.In your case,
appleandmodalare both refs, so you should useapple.valueandmodal.valueto access and modify their values. The corrected code reflects this adjustment, eliminating the attempt to usethiswithin thedefineExposefunction.In the
defineExposefunction, you can directly reference theappleref without usingthis. Here's the corrected code: