Vue emitting events from plugins

Viewed 1393

Let's say I have a simple Vue plugin that has no components it just provides some methods to the user:

export default {
    install(Vue, options) {
        // Unrelated stuff here.
    }

    Vue.prototype.$foo = () => {
        // Emit an event here.
    }
}

When the user calls the foo method I would like to emit an event for them to respond to but I'm not sure if there is a Vue way to do this or if I just have to use a CustomEvent.

1 Answers

Just normally...

export default {
    install(Vue, options) {
        this.$on('event-name', () = {});
    }

    Vue.prototype.$foo = () => {
        this.$emit('event-name');
    }
}
Related