I’m having a little trouble with using checkboxes as a separate component. I just have a simple child component that contains a template for a card with some data and also a checkbox, like a product list, each product card should have a checkbox and once selected the user should be able to delete it. Right now the way I’ve written the code, depending on how many items are in an array I’m looping through it and displaying cards, but if I check one checkbox, the second checkbox is also checked, even though I’m giving the input an individual id when rendered. Couldn’t find a proper solution anywhere so I’d appreciate any help! This is the child component : ProductCard.vue
<template>
<div>
<input :id="id" type="checkbox" :checked="modelValue"
@change="$emit('update:modelValue',$event.target.checked)">
</div>
</template>
<script setup>
defineProps(['id','modelValue'])
</script>
and this is the parent component :
<template>
<ProductCard v-for="item in array" :key="item.id" v-model="newsletter"/>
</template>
<script setup>
import ProductCard from '@/components/ProductCard.vue'
const array = [{},{}];
</script>
Thanks in advance!
2
Answers
Have you tried this?
Haven’t used vue in a while so it could be wrong
You can achieve this by passing a unique
v-model
prop to eachProductCard
component.