Skip to content

Bus

The $bus is a wrapper of mitt a simple event emitter.

Usage

vue
<script>
export default {
  mounted() {
    // start listening
    this.$bus.on('<your-channel>', this.busListener)
  },
  beforeDestroy() {
    // stop listening
    this.$bus.off('<your-channel>', this.busListener)
  },
  methods: {
    busListener(data) {
      // do something with data
    },
    emitOnBus(data) {
      // emit an event on the listeners
      this.$bus.emit('<your-channel>', { ...data })
    },
  }
}
</script>

The two methods, on and off are used respectively to add/remove a listener for an event.

The method emit is used to emit an event on the listeners.

To know more about mitt, read the documentation on github