Created
September 3, 2017 04:03
-
-
Save patleeman/7b48e9a5641b45f64a916bb9965d7939 to your computer and use it in GitHub Desktop.
Vue.js global event bus example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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