Array mutations in Vue.js are not automatically detected when using direct index
access to change an item or sub-field within it.
To address this issue, you can utilize the set
method of the Vue
object:
// Vue.set
const index = 0;
const newValue = { ...this.users[index], name: 'Mary' };
Vue.set(this.users, index, newValue);
Alternatively, you can manipulate the array using the splice
method, which is internally overridden by Vue.js:
const index = 0;
const newValue = { ...this.users[index], name: 'Mary' };
// Using array manipulation
this.users.splice(index, 1, newValue);
Another approach is to follow the immutable data practice like so:
const newArray = [...this.users];
const newValue = { ...this.users[0], name: 'Mary' };
newArray[0] = newValue;
this.users = newArray;