I have a VueJs component that will be added by user several times in a page by clicking on a button, it has v-for directive on the component on the main page. inside the component I have a event listener like this:
It is not clear what you're trying to achieve; maybe the keydown event listener needs to be registered on the parent component rather than on the dynamic children?
@armani What does the component actually do on a keydown event? As it may be better to just emit an event from those components to a common parent component:
<script setup>
const onKeyDown = (key) => {
// Do something when a key is pressed in a child-component...
};
</script>
<template>
<parent-component>
<child-component v-on:key-down="onKeyDown"></child-component>
<child-component v-on:key-down="onKeyDown"></child-component>
<child-component v-on:key-down="onKeyDown"></child-component>
</parent-component>
</template>
If this is just listening for keypresses from custom inputs, then you’d probably be better off using v-model instead of trying to listen for keydown events.