Looking for a solution in my Vue.js SPA, where I have a specific page requiring keyboard interaction and using Vue Router for navigation.
Currently, I have set up the following to handle keyboard events:
const interactiveComponent = {
//
methods: {
handleKeyboard(event) {
// do something
}
},
created() {
document.addEventListener('keydown', this.handleKeyboard);
},
//
};
The issue arises when leaving the page as the handler remains active.
An even bigger problem occurs when revisiting the page, causing the handler to register multiple times and triggering the event more than once.
Is there a recommended convention in Vue.js to address this type of persistent handler?