3👍
✅
You can use computed property names. Documentation
new Vue({
el: "#app",
data: {
counter: 0,
anothercounter: 5
},
methods: {
addone: function(c) {
this[c] = this[c] + 1;
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<p>
counter is {{ counter }} : <button @click="addone('counter')">+1</button>
</p>
<p>
anothercounter is {{ anothercounter }} : <button @click="addone('anothercounter')">+1</button>
</p>
</div>
Source:stackexchange.com