Skip to content

Instantly share code, notes, and snippets.

@ahmadshah
Created June 24, 2016 07:49
Show Gist options
  • Save ahmadshah/e9cdd7eae67249511dd8adaf664b82b8 to your computer and use it in GitHub Desktop.
Save ahmadshah/e9cdd7eae67249511dd8adaf664b82b8 to your computer and use it in GitHub Desktop.
Vuejs Event Emitter
<template>
<button @click="emitEvent">EVENT</button>
</template>
<script>
import { EV } from './events'
export default {
methods: {
emitEvent() {
EV.emit('sample-event', 'foobar')
}
}
}
</script>
<template>
<component-a></component-a>
<br>
<div>{{ msg }}</div>
</template>
<script>
import { EV } from './events'
import ComponentA from './componentA.vue'
export default {
data() {
return {
msg: null
}
},
created() {
EV.on('sample-event', (foo) => {
this.$set('msg', foo)
})
}
}
</script>
import { EventEmitter } from 'events'
export const EV = new EventEmitter
@slenty
Copy link

slenty commented Jun 20, 2018

But in events.js, it should be like this
const EventEmitter = require('events')
export const myEmitter = new EventEmitter()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment