[Vuejs]-Event_Bus vue variable access to single file component Vue.js – Laravel Mix

3👍

Just use this:

window.EventBus = new Vue();
👤Pavel

2👍

You just defined the EventBus in the app.js file.

It is not accessible in the TransactionList.vue component as it has no reference to the EventBus

So export the EventBus in your app.js as

export const EventBus = new Vue();

And then import the EventBus in your components script as

<script>
    import {EventBus} from './path/to/app.js'
    export default {

    }
</script>

See es6 import and export

1👍

Create the event bus in it’s own file:

// bus.js
import Vue from 'vue'
const bus = new Vue()
export default bus

The import it into the components you want to use it in:

import bus from 'bus'
export default { 
   ...
   methods: {
       myMethod () {
           bus.$emit('my-event', 'test')
       }
    }
   ...
}

Leave a comment