Home > Web Front-end > JS Tutorial > Analysis of changes in Vue detection objects and arrays

Analysis of changes in Vue detection objects and arrays

不言
Release: 2018-06-30 16:14:11
Original
1505 people have browsed it

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
   }
 }
})
Copy after login

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)
Copy after login

such as

$set(data111, b, 2);
Copy after login

or:

$set(key,value)
Copy after login

such as

vm.$set(‘b', 2);
Copy after login

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 )
Copy after login

Usage:

this.$set(this.arr,0,12)
Copy after login

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!

Related labels:
vue
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template