Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mesqueeb's avatar

How to catch a child's $emit in the parent with Vue?

I have a Vue component with a child component that creates a confirmation message box.

I would capture e.g. the cancel button on the confirmation child component like this:

// This is part of my child component's HTML template:
<a href="" @click="popoutCall('cancel', popout)">Cancel</a>

Then I'd emit the event like this:

// This is a method in my child component:
popoutCall(msg, popout){
        this.$emit(msg, popout.item.id);
},

Now I want to catch this event on in my main Vue instance, knowing that the confirmation box for the item with a certain ID was 'cancelled'. How do I catch this event on the Vue parent and do something like console.logging this ID that was emitted with the event?

Or if anyone has any advice on how to best create a confirmation dialogue and handling the chosen option, let me know!

0 likes
21 replies
mesqueeb's avatar

I want to start using version Vue.js v.2.

So I read the docs here: http://vuejs.org/guide/components.html#Using-v-on-with-Custom-Events

But the example they give to catch an emission is in the HTML: <child v-on:increment="incrementTotal">.

However, I cannot seem to find out how to capture the ID I want to send alongside my child emission to the parent. So instead of solving your event capture in the HTML, isn't there any way to capture in the main Vue Object's options?

mesqueeb's avatar

Dear @zachleigh Thank you, this is exactly what I've done for emitting it, but the problem is I don't know how to capture it properly, together with my item ID in my main Vue instance. Do you know how to capture this event on the top level vue root and do something with it?

1 like
zachleigh's avatar

You need an events option on your Vue instance:

    events: {
        eventName: function (argument) {
            // logic
        },
    },
jekinney's avatar

I use events. So dispatch the data and capture with event as noted above.

You can also set a prop as two way. Meaning the data passed in to a child will be synced to the parent on change.

tiagomatosweb's avatar

Hi, I just figured out! Firstly, just to ansewer @jekinney events/dispatch/broadcast was replaced by $emit and $on on Vuejs 2 as follows http://vuejs.org/guide/migration.html#dispatch-and-broadcast-replaced. @gocanto vuex e more for complexity approach and yes, it is more indicated uso. However, for very simple approach doesn't make much sense.

So, back to Vuejs 2, $emit and $on are used for the same component, not between them. See this http://vuejs.org/guide/components.html#Non-Parent-Child-Communication

To use $emit and $on for different component you should listen the event by v-on. as following http://vuejs.org/guide/components.html#Using-v-on-with-Custom-Events

To pass data through $emit you just need to use vm.$emit('eventName', arg) in the child component and then use someMethod(arg) {} in the parent component.

Have a look at this example I've done: https://jsfiddle.net/tiagomatosweb/vqtnpyzw/

Hope I've helped!

6 likes
kenny11's avatar

what about parent to child?

The "Hub" which uses a Vue instance as an event hub is the only way?

gocanto's avatar

The hub is for smaller application, but for others is beter use vuex

angelarted's avatar

want to thank @tiagomatosweb for the jsfiddle example! Couldn't find a way to understand this intricate system of multiple functions calling each other, and with you example I found it out! Big thank!

fluksvue's avatar

Is it possible to emit an event from a child of a child (multiple component layers below) to call a method on the main vue instance?

2 likes
tiagomatosweb's avatar

Hey, if the components are parent/child you should use $emit to emit the data and v-on to catch the data. Even if you have child of a child i think it is advised to use this methodology.

In you scenario, you would use $emit from the second child, catch the data in the first child using v-on. After that, you would $emit again from the first child and finally catch it using v-on in the parent component.

If the components aren't parent/child you could use "bus", found here https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication

If you logic is too complex, I would suggest using state management found here https://vuejs.org/v2/guide/state-management.html.

parvezmrobin's avatar

The first param of $emit will be the name of the event and the second param will be the param of the event. Firstly you may do something like this in the component.

$emit('evenname', id)

Then catch it in the parent and call corresponding method of parent.

<child @eventname="eventHandler(id)"></child>

Then just add a method named eventHandler(id) in the methods option in parent Vue element.

5 likes
Javi's avatar

Any params you emit from the child will be sent to the parent automatically, if you define it explicitelly it will be recognized as undefined. I mean, for any further reader, change

<child @eventname="eventHandler(id)"></child>

to

<child @eventname="eventHandler"></child>
deathemperor's avatar

@parvezmrobin's resolution works but it must be defined as this

<child @eventname="eventHandler($event)"></child>

$event is the 2nd parameter you pass on this.$emit()

1 like

Please or to participate in this conversation.