Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

Vue emitting events from plugins

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.

Upvotes: 2

Views: 1791

Answers (1)

McKabue
McKabue

Reputation: 2232

Just normally...

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

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

Upvotes: 1

Related Questions