Javascript VueJs对象数组监视程序在对象属性更改时不更新
我的状态中有一个对象数组,如下所示:Javascript VueJs对象数组监视程序在对象属性更改时不更新,javascript,vue.js,Javascript,Vue.js,我的状态中有一个对象数组,如下所示: data(){ 返回{ 用户:[{id:1,名称:'bob'},{id:2,名称:'bill'}] } } 当我这样更改数据时: this.users[0]。name='Mary' 我为students属性设置的观察程序没有运行,如何使其运行?Vue.js无法在您使用直接索引访问更改项目的任何项目或子字段时检测到数组的突变 为此,可以使用Vue对象的set方法: // Vue.set const index = 0; const newValue = {
data(){
返回{
用户:[{id:1,名称:'bob'},{id:2,名称:'bill'}]
}
}
当我这样更改数据时:
this.users[0]。name='Mary'
我为
students
属性设置的观察程序没有运行,如何使其运行?Vue.js无法在您使用直接索引
访问更改项目的任何项目或子字段时检测到数组的突变
为此,可以使用Vue
对象的set
方法:
// Vue.set
const index = 0;
const newValue = { ...this.users[index], name: 'Mary' };
Vue.set(this.users, index, newValue);
或者,您可以简单地使用Vue.js内部覆盖的splice
方法操纵数组:
const index = 0;
const newValue = { ...this.users[index], name: 'Mary' };
// Using array manipulation
this.users.splice(index, 1, newValue);
或者,您可以使用不可变数据实践作为:
const newArray = [...this.users];
const newValue = { ...this.users[0], name: 'Mary' };
newArray[0] = newValue;
this.users = newArray;
首先,在你的问题(代码)中没有所谓的
学生
,其次,没有观察者…(至少在你的问题中)参见