Modified1 year, 3 months ago. I'm new to Vuejs. Made something, but I don't know it's the simple / right way. I want some dates in an array and update them on a event. First I tried Vue.set, but it dind't work out. Now after changing my array item:. I push() nothing to the array and it will update. But sometimes the last item will be hidden, somehow..
I think this solution is a bit hacky, how can I make it stable? Simple code is here:.
- 44 gold badges2525 silver badges3535 bronze badges. 3535 bronze badges. 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 nor the .length property of an array.
5858 bronze badges.
8,46466 gold badges3434 silver badges5555 bronze badges. 3434 silver badges5555 bronze badges. 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. For vuex you will want to do Vue.set(state.object, key, value).
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.
22 gold badges88 silver badges1111 bronze badges. 1111 bronze badges. As stated before - VueJS simply can't track those operations(array elements assignment).All operations that are tracked by VueJS with array are here.But I'll copy them once again:.
During development, you face a problem - how to live with that :). push(), pop(), shift(), unshift(), sort() and reverse() are pretty plain and help you in some cases but the main focus lies within the splice(), which allows you effectively modify the array that would be tracked by VueJs.So I can share some of the approaches, that are used the most working with arrays.
You need to replace Item in Array:. Note: if you just need to modify an item field - you can do it just by:.
And this would be tracked by VueJS as the item(Object) fields would be tracked. You need to empty the array:. Actually you can do much more with this function splice() - w3schools linkYou can add multiple records, delete multiple records, etc.
Vue.set() and Vue.delete(). Vue.set() and Vue.delete() might be used for adding field to your UI version of data.
For example, you need some additional calculated data or flags within your objects. You can do this for your objects, or list of objects(in the loop):.