I am currently dealing with a component that generates three items (boxes) each containing three buttons (created using v-for). The issue I am facing is that when I click on a button, all the numbers inside the buttons change simultaneously. I want to be able to change the number in each button individually upon clicking.
<template>
<div>
<div class="box" v-for="message in messages" v-bind:key="message">
<p>{{ message }}</p>
<button class="btn" v-on:click="updateCounter(message)">{{ getCounterValue(message) }}</button>
</div>
</div>
</template>
<script>
export default {
name: "childComponent",
data: function () {
return {
messages: ["item 1", "item 2", "item 3"],
counters: { "item 1": 0, "item 2": 0, "item 3": 0 },
};
},
methods: {
updateCounter(message) {
this.counters[message]++;
},
getCounterValue(message) {
return this.counters[message];
},
}
};
</script>
<template>
<div class="container">
<h2>I am the parent component</h2>
<child-component />
</div>
</template>
<script>
import childComponent from "./Hijo.vue";
export default {
name: "fatherComponent",
components: {
childComponent,
},
};
</script>
I have been working all day trying to solve this issue between the father and child components but I am struggling to find a solution.