William Ng
William Ng

Reputation: 52

Whats wrong with my eventBus implementation?

I want to trigger a function from another component which is not parents and child relationship. i try to use eventbus, but i am not sure am i implemented it correctly, since the function is not working. i have created a boot file eventBus.js and added it into the quasar config, but its still not working. Anyone can help me to check which part did i implemented wrongly?

eventBus.js
import Vue from 'vue'

const eventBus = new Vue();

export default eventBus

Vue.prototype.$eventBus = eventBus;

below is the function that i want to get from this page, (this.getSchedule(), it is working perfectly on current page).

mounted(){
    this.$eventBus.$on("refreshListSchedule", ()=>{

      scheduleDate = this.scheduleDate
      console.log(scheduleDate)
      console.log("Testing successed")
      this.getSchedule()
    })
},

The page that i want to trigger it

this.$eventBus.$emit('refreshListSchedule')

Upvotes: 0

Views: 533

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14259

It is better if you manually import from eventBus.js in each component where you want to emit or subscribe for events - don't rely on Vue.prototype.

And don't forget to unsubscribe in the beforeDestroy hook.

Upvotes: 1

Related Questions