Vue中数组或对象更新后页面未刷新,如何处理?
- 内容介绍
- 文章标签
- 相关推荐
本文共计327个文字,预计阅读时间需要2分钟。
在使用数组时,数组内部的数据会发生改变,但与数组绑定的页面数据并未发生变化。例如:
{{ item.name }}
在使用数组的时候,数组内部数据发生改变,但是与数组绑定的页面的数据却没有发生变化。
<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,导致页面得数据不会改变。
有三种解决方式。
本文共计327个文字,预计阅读时间需要2分钟。
在使用数组时,数组内部的数据会发生改变,但与数组绑定的页面数据并未发生变化。例如:
{{ item.name }}
在使用数组的时候,数组内部数据发生改变,但是与数组绑定的页面的数据却没有发生变化。
<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,导致页面得数据不会改变。
有三种解决方式。

