Reasons and solutions for not being able to detect array changes in Vue2

Reasons and solutions for not being able to detect array changes in Vue2

Due to limitations of JavaScript, Vue cannot detect changes to the following arrays:

  • When setting an array item directly using an index, for example: vm.items[indexOfItem] = newValue
  • When modifying the length of an array, for example: vm.items.length = newLength
var vm = new Vue({
  data: {
    items: ['a', 'b', 'c']
  }
})
vm.items[1] = 'x' // not responsive vm.items.length = 2 // not responsive

Workaround

Manually add monitoring // Vue.set
Vue.set(vm.items, indexOfItem, newValue)
vm.$set(vm.items, indexOfItem, newValue)
Use the array mutation method, because Vue implements responsiveness for array mutation methods // Array.prototype.splice
vm.items.splice(indexOfItem, 1, newValue)

Why can't I monitor changes in the two arrays in Vue2.0?

The official documentation briefly summarizes these two points as being impossible to implement "due to JavaScript limitations", and Object.defineProperty is a solution for detecting data changes. Does this limitation refer to Object.defineProperty?

In fact, the reason is not because of the vulnerability in Object.defineProperty(), but because of performance considerations. The performance of Object.defineProperty in an array is the same as that in an object. The index of the array can be regarded as the key in the object.

  • When accessing or setting the value of the corresponding element by index, the getter and setter methods can be triggered
  • Pushing or unshifting will increase the index. Newly added attributes need to be initialized manually before they can be observed.
  • Deleting an element by pop or shift will delete and update the index, and will also trigger the setter and getter methods.

Therefore, Object.defineProperty has the ability to monitor array index changes, but vue2.x has abandoned this feature.

Source code analysis

Object.property can detect operations that change arrays through indexes, but Vue does not implement this. Then let's look at the source code:

Vue 3.0

In Vue 3.0, proxy is used instead of Object.defineProperty() to solve the existing problems.

The above is the detailed content of the reasons why array changes cannot be detected in Vue2 and the solution. For more information about Vue2 array changes, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue2 responsive system: deep response
  • Nesting of Vue2 responsive system
  • Vue2 responsive system branch switching
  • Introduction to Vue2 Responsive System
  • Vue2 responsive system asynchronous queue
  • Implementation of the vue2.x array hijacking principle
  • Vue2 responsive system array

<<:  Installation and use of mysql on Ubuntu (general version)

>>:  How to store text and pictures in MySQL

Recommend

Vue implements local storage add, delete and modify functions

This article example shares the specific code of ...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Summary of MySQL character sets

Table of contents Character Set Comparison Rules ...

Detailed explanation of MySQL database (based on Ubuntu 14.0.4 LTS 64 bit)

1. Composition and related concepts of MySQL data...

Summary of Textarea line break issues in HTML

Recently, I encountered a problem of whether the d...

In-depth analysis of MySQL query interception

Table of contents 1. Query Optimization 1. MySQL ...

WeChat applet custom menu navigation to achieve staircase effect

Design Intentions When developing a page, you oft...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...

CSS clear float clear:both example code

Today I will talk to you about clearing floats. B...

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getti...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

Pure HTML+CSS to achieve Element loading effect

This is the effect of the Element UI loading comp...

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...

JavaScript MouseEvent Case Study

MouseEvent When the mouse performs a certain oper...

HTML4.0 element default style arrangement

Copy code The code is as follows: html, address, ...