在使用數組的時候,數組內部數據發生改變,但是與數組綁定的頁面的數據卻沒有發生變化。
<ul>
<li v-for="(item,index) in todos" :key="index">{{item.name}}</li>
</ul>
data () {
return {
msg: 'Welcome to Your Vue.js App',
todos: [{
name: 'aa',
age: 14
}, {
name: 'bb',
age: 15
}, {
name: 'cc',
age: 16
}],
obj: {name: 'lihui', age: 17}
}
},
methods: {
changeTodos: function () {
var _this = this
_this.todos[0] = {
name: 'zhangsan',
age: 15
}
console.log(this.todos)
/*
this.$set(this.todos, 0, 'nn')
this.$forceUpdate()
*/
}
這種修改得方式,無法出發數組得set,導致頁面得數據不會改變。有三種解決方式。
一、使用全局得set方法。
this.$set(this.todos,0,{name: 'zhangsan',age: 15});或者對象this.$set(this.obj,'key',value);
二,強制更新
this.$forceUpdate()
