Vuejs and Vue.set(), update array Vuejs and Vue.set(), update array vue.js vue.js

Vuejs and Vue.set(), update array


EDIT 2

  • For all object changes that need reactivity use Vue.set(object, prop, value)
  • For array mutations, you can look at the currently supported list here

EDIT 1

For vuex you will want to do Vue.set(state.object, key, value)


Original

So just for others who come to this question. It appears at some point in Vue 2.* they removed this.items.$set(index, val) in favor of this.$set(this.items, index, val).

Splice is still available and here is a link to array mutation methods available in vue link.


VueJS can't pickup your changes to the state if you manipulate arrays like this.

As explained in Common Beginner Gotchas, you should use array methods like push, splice or whatever and never modify the indexes like this a[2] = 2 nor the .length property of an array.

new Vue({  el: '#app',  data: {    f: 'DD-MM-YYYY',    items: [      "10-03-2017",      "12-03-2017"    ]  },  methods: {    cha: function(index, item, what, count) {      console.log(item + " index > " + index);      val = moment(this.items[index], this.f).add(count, what).format(this.f);      this.items.$set(index, val)      console.log("arr length:  " + this.items.length);    }  }})
ul {  list-style-type: none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.11/vue.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script><div id="app">  <ul>    <li v-for="(index, item) in items">      <br><br>      <button v-on:click="cha(index, item, 'day', -1)">      - day</button> {{ item }}      <button v-on:click="cha(index, item, 'day', 1)">      + day</button>      <br><br>    </li>  </ul></div>