Skip to content

Instantly share code, notes, and snippets.

@patleeman
Created September 3, 2017 04:03
Show Gist options
  • Save patleeman/7b48e9a5641b45f64a916bb9965d7939 to your computer and use it in GitHub Desktop.
Save patleeman/7b48e9a5641b45f64a916bb9965d7939 to your computer and use it in GitHub Desktop.
Vue.js global event bus example
import Vue from 'vue'
// Create the event bus by creating a new Vue instance and
// binding it somehwere accessible. If you bind it to the
// Vue prototype, you can access it within your components
// like this:
//
// Emit an event
// this.$bus.$emit('myEvent', {data: true})
//
// React to an event
// this.$bus.$on('myEvent', (message) => {console.log(message})
Vue.prototype.$bus = new Vue({})
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment