Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript VueJs对象数组监视程序在对象属性更改时不更新_Javascript_Vue.js - Fatal编程技术网

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;

首先,在你的问题(代码)中没有所谓的
学生
,其次,没有观察者…(至少在你的问题中)参见