Due to limitations of JavaScript, Vue cannot detect changes to the following arrays:
var vm = new Vue({ data: { items: ['a', 'b', 'c'] } }) vm.items[1] = 'x' // not responsive vm.items.length = 2 // not responsive WorkaroundManually 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.
Therefore, Object.defineProperty has the ability to monitor array index changes, but vue2.x has abandoned this feature. Source code analysisObject.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.0In 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:
|
<<: Installation and use of mysql on Ubuntu (general version)
>>: How to store text and pictures in MySQL
This article example shares the specific code of ...
Table of contents 1. Prerequisites 1.1 Supported ...
Table of contents Character Set Comparison Rules ...
1. Composition and related concepts of MySQL data...
Recently, I encountered a problem of whether the d...
Table of contents 1. Query Optimization 1. MySQL ...
Design Intentions When developing a page, you oft...
Create a new project test1 on Code Cloud Enter th...
Today I will talk to you about clearing floats. B...
I just joined a new company recently. After getti...
Table of contents 1. Specify different packaging ...
This is the effect of the Element UI loading comp...
Table of contents Asynchronous traversal Asynchro...
MouseEvent When the mouse performs a certain oper...
Copy code The code is as follows: html, address, ...