[Vuejs]-Vuex commit commits only the last value of an array from a response

0👍

https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

Vuex is a plugin of Vue, so reactivity of Vuex’s state follows same rules as reactive property in Vue. In your case, since the property you update is an array of objects, Vue only detect changes in size of array, not details of each item in array. So if you need to trigger updates in view layer, you have to use Vue.set instead:

import Vue from 'vue'

...

fetchVotes(state, payload) {
  Vue.set(state, 'votes', payload)
},

and if you need to delete items from your array, use Vue.delete instead of delete.

Leave a comment