I have a scenario where I have several buttons on a page, and when I click one to toggle its text, all of the buttons change. How can I utilize VUE to isolate functionality and make each button's @click
event only affect the clicked button? In the code snippet below, you can see that every button changes when clicked, whereas I want only the text of the clicked button to change.
Here is an example from another post, full credit goes to:
change button while pressing it with vue.js
new Vue({
el: '#app',
data: {
isFavorite: false
},
methods: {
toggleFavorite() {
this.isFavorite = !this.isFavorite;
}
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<button @click="toggleFavorite" v-show="!isFavorite">Add to favorites</button>
<button @click="toggleFavorite" v-show="isFavorite">Delete from favorites</button>
<button @click="toggleFavorite" v-show="!isFavorite">Add to favorites</button>
<button @click="toggleFavorite" v-show="isFavorite">Delete from favorites</button>
</div>