Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  189] [ 6]  / answers: 1 / hits: 42001  / 3 Years ago, mon, july 5, 2021, 12:00:00

I have the following component:


<SomeModal :is-modal-active="isAddingThing" @close="isAddingThing = false" />

Inside that component looks like this:


<script setup>
import { defineProps } from 'vue'

const props = defineProps({
isModalActive: Boolean,
})

const handleClose = () => {
emit('close') // doesn't work
}
</script>

<template>
<V-Modal @close="handleClose">
...
</V-Modal>
</template>

How do I emit to parent?


More From » vuejs3

 Answers
26

Update 2022


The useContext and getCurrentInstance APIs were deprecated and are no longer exposed to the Vue 3 API. Instead of that, you should use the defineEmits at the root level on the setup script.


<template>
<button @click="action('🎉')"></button>
</template>
<script setup>
const emit = defineEmits(['close', 'unClose'])
const action = (id) => emit('close', val);
</script>

and for typescript the definition would be:


const emit = defineEmits<{
(e: 'close', val: string): void
(e: 'unClose', id: number): void
}>()



Outdated, Pre-Vue 3.2



  • You don't need to define emit at all to run $emit from the template

  • useContext provides the emit function for use in setup function

  • using defineEmits adds the list of emits to the component (equivalent to emits: definition of component) but doesn't help with the emit




useContext


<template>
<button @click="action('🎉')"></button>
<!-- emit is defined through useContext -->
<button @click="emit('action1','🥑')">1</button>
<!-- $emit here doesn't need to be defined -->
<button @click="$emit('action2','🦄')">2</button>
</template>
<script setup>
import { defineProps, useContext } from 'vue'
const { emit } = useContext()
const action = (id) => emit('action', id);
</script>

Note that vetur extension might tell you that it's deprecated and that you should use useSlots and useAttrs instead. If you want to use emit from the script, those, obviously, won't help with that.


getCurrentInstance


import { defineProps, getCurrentInstance} from 'vue'
const { emit } = getCurrentInstance()

In addition to running an emit, vue3 allows (recommends🤷‍♂️) that the emits are defined, so the component parent might know what events a component might emit. To do that using the <script setup> form, you can use defineEmit


<script setup>
import { defineEmit } from 'vue'
const emit = defineEmit(['action'])
const action = (id) => emit('action', id);
</script>

This will allow sending action emit event, but if you emit a doit although it will still emit, you'll get a warning in the console



[Vue warn]: Component emitted event "doit" but it is neither declared in the emits option nor as an "onDoit" prop.



[#50242] Saturday, June 5, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;