-
-
Save slenty/ed451c4e99e21595609c64aef84afb38 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