Last active
March 21, 2020 17:25
-
-
Save Lelectrolux/71673544452a9911934ce935dc4ac39b to your computer and use it in GitHub Desktop.
Renderless (global) event listener component https://www.youtube.com/watch?v=qA2RfGbgNzk https://github.com/shentao/vue-global-events
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> | |
<div> | |
<event-listener @keydown.space="doSomething"> | |
... | |
</div> | |
</template> | |
<script> | |
export default { | |
methods: { | |
doSomething () { | |
// Start-stop a video for example | |
} | |
} | |
} | |
</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
export default { | |
render () {}, | |
mounted () { | |
Object.keys(this.$listeners) | |
.foreach(event => { | |
window.addEventListener(event, this.$listeners[event]) | |
}) | |
}, | |
destroyed () { | |
Object.keys(this.$listeners) | |
.foreach(event => { | |
window.removeEventListener(event, this.$listeners[event]) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment