Home > Article > Web Front-end > Analysis of changes in Vue detection objects and arrays
This article shares with you the relevant knowledge points and example codes for Vue to detect changes in objects and arrays. Friends who are interested can refer to it.
In JavaScript, objects and arrays are reference types, pointing to the same memory space. If prop is an object or array, changing it inside the child component will affect the state of the parent component. You can modify objects or arrays directly in subcomponents, but changes will not occur if the data changes.
Detecting object changes
1. Unable to detect the addition or deletion of object attributes
var vm = new Vue({ data:{ data111:{ a = 1 } } })
data111.a = 2;//This can cause changes
But data111.b = 2; and vm.b = 2 cannot detect changes
Required Use
Vue.set(object, key, value)
such as
$set(data111, b, 2);
or:
$set(key,value)
such as
vm.$set(‘b', 2);
to detect the array Change
Changes cannot be detected in the following two situations:
1. Set the element directly through the index, such as arr[0]=12;
2. Directly modify the length of the array, such as vm.arr.length
Vue.set( object, key, value )
Usage:
this.$set(this.arr,0,12)
The above is the entire content of this article, I hope it will be helpful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Analysis of vue data control view source code
Introduction to Vue dynamically setting routing parameters
The above is the detailed content of Analysis of changes in Vue detection objects and arrays. For more information, please follow other related articles on the PHP Chinese website!