If you are looking to create an event bus for events emit & listening throughout your Nuxt application here is the way you can do that.

create bus.js in the plugins directory

import Vue from 'vue'

const eventBus = {}

eventBus.install = function (Vue) {
  Vue.prototype.$bus = new Vue()
}

Vue.use(eventBus)


now register your plugin in nuxt.config.js

/*
** Plugins to load before mounting the App
*/
plugins: [
  '~/plugins/bus'
],

That’s it. now use it anywhere
 

// Event emit
this.$bus.$emit('test-event')
or
app.$bus.$emit('test-event')

// Event Listen
this.$bus.$on('test-event', () => {})

Leave a Comment

Frontend Developers

Useful Pages

Copyright © 2021 Design by BinBytes