Created
June 24, 2016 07:49
-
-
Save ahmadshah/e9cdd7eae67249511dd8adaf664b82b8 to your computer and use it in GitHub Desktop.
Vuejs Event Emitter
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
<template> | |
<button @click="emitEvent">EVENT</button> | |
</template> | |
<script> | |
import { EV } from './events' | |
export default { | |
methods: { | |
emitEvent() { | |
EV.emit('sample-event', 'foobar') | |
} | |
} | |
} | |
</script> |
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
<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> |
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 { EventEmitter } from 'events' | |
export const EV = new EventEmitter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But in events.js, it should be like this
const EventEmitter = require('events')
export const myEmitter = new EventEmitter()