Vue.js fire event in component

To fire an event in a component and respond to it in the parent.

In the component, inside “methods” create a method with this.$emit

e.g.

methods: {
    my_event(somedata) {
        this.$emit('my_event', somedata);
    }
}

Trigger this somehow

e.g.

<a href="javascript:;" v-on:click="my_event(some-data)">Some action</a>

Then in the parent create a method to respond to this

methods: {
    OnMyEvent() {
        //do something
    }
}

And then wire the event up on the component markup

<my-component v-on:my_event="OnMyEvent"></my-component>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.