Using the eventBus pattern in Vue.js allows a central place to emit events so that subscribing listening components can handle such events.
The below code snippets are setting up listeners on child components to receive an updated Object server
when a particular UI change happens.
I ran into something today where this DID'T work in a child component:
created: function() {
eventBus.$on('serverSelected', function(server) {
console.log('serverDetails, server=' + server.toString());
this.server = server;
});
},
but this DID work:
created: function() {
eventBus.$on('serverSelected', (server) => {
console.log('serverDetails, server=' + server.toString());
this.server = server;
});
},
I believe the only different is the ES6 syntax for the callback. But the vanilla JS should still work right?
I'm very new to JS. Why is there a different and why does only the second version work?