Test array changing
by Max Sinev
HTML
<div id="app">
<div v-for="todo in todos" :key="todo.text">
{{todo.text}}
</div>
<button @click="changeItemProperty">changeItemProperty(works)</button>
<button @click="changeItemWithAssign">changeItemWithAssign(works)</button>
<button @click="changeItem">changeItem(does not work!)</button>
<button @click="changeItem2">changeItem2(works)</button>
</div>
Vue
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript" },
{ text: "Learn Vue" },
{ text: "Play around in JSFiddle" },
{ text: "Build something awesome" }
]
},
methods: {
// work because object property is reactive
changeItemProperty() {
this.todos[3].text = "changedItemProperty";
},
// same reason, properties are reactive
changeItemWithAssign() {
Object.assign(this.todos[3], { text: "changedItemWithAssign" });
},
// does not work, can not track changes in array
// also this will brake all attempts to change TEXT property in UI
// because objects property after this is not reactive
changeItem() {
this.todos[3] = { text: "changedItem" }
},
// works
changeItem2() {
Vue.set(this.todos, 3, { text: "changedItem2" });
}
}
})