Test array changing
by Max Sinev
HTML
<div id="app">
<div v-for="todo in todos" :key="text">
{{todo.text}}
</div>
<button @click="changeItemProperty">changeItemProperty</button>
<button @click="changeItemWithAssign">changeItemWithAssign</button>
<button @click="changeItem">changeItem</button>
<button @click="changeItem2">changeItem2</button>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
Vue
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", done: false },
{ text: "Learn Vue", done: false },
{ text: "Play around in JSFiddle", done: true },
{ text: "Build something awesome", done: true }
]
},
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", done: true });
},
// does not work, can not track changes in array
changeItem() {
this.todos[3] = { text: "changedItem", done: true }
},
// works
changeItem2() {
Vue.set(this.todos, 3, { text: "changedItem2", done: true });
}
}
})