Home > OS >  NuxtJS and event bus
NuxtJS and event bus

Time:03-20

I'm trying to create an event from an instance property in nuxt, however the event is not emitted or received.

plugins/internal/bus.js

import Vue from 'vue';

const eventBus = {}

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

Vue.use(eventBus)

plugins/internal/index.js

import Vue from 'vue';

export default function (_ctx, inject) {
  const notify  = function (msg) {

    console.log('emitting', msg);

    setInterval(() => {
      this.$bus.$emit('add', msg);
    }, 500);
  }
   .bind(new Vue());

  inject('notify', notify);
}

nuxt.config.js

  plugins: [
    '~/plugins/internal/bus.js',
    '~/plugins/internal/index.js',
    ...
  ]

And in my component,

  mounted() {
    this.$bus.$on('add', (val) => {
      console.log(val);
    })
    this.$bus.$on('close', this.onClose)
  },

Doing this.$notify({ foo: 'bar' }), calls the instance property correctly, however either the event is not emitted or is not received, frankly not sure how to debug this. What am I missing here?

CodePudding user response:

At the end, the issue was coming from a component (Notification) that was not properly registered.

  • Related