[Vuejs]-In Vue, can I update a data attribute passed to a method so it then updates the UI?

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>

Leave a comment