In the process of using Vue to develop projects, you often encounter this problem: when an object or array (the value in the array is an object) is declared or assigned in Vue's data, adding a new attribute to the object, if the value of this attribute is updated, the view will not be updated. This is because the newly added attributes are not responsive, so they will not trigger the view update. Usually, the static method Vue.set() or the instance method this.$set() is used to solve it. Usage: Object: this.$set(target, key, value) Array: this.$set(target, index, value) But whether it is the static method Vue.set() or the instance method this.$set(), their underlying implementation logic is the same, and the implementation logic is as follows: /** * Set a property on an object. Adds the new property and * triggers change notification if the property doesn't * already exists. */ export function set (target: Array<any> | Object, key: any, val: any): any { // First, determine if the target object passed in is undefined, null, primitive (original value), or throw a warning if (process.env.NODE_ENV !== 'production' && (isUndef(target) || isPrimitive(target)) ) { warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`) } // Check if the target object target is an array and key is a valid index if (Array.isArray(target) && isValidArrayIndex(key)) { // Take the larger value of the target array's length and key as the target's length attribute target.length = Math.max(target.length, key) //Replace the element at the key position through splice target.splice(key, 1, val) return val } // If key already exists in the target object, assign it directly if (key in target && !(key in Object.prototype)) { target[key] = val return val } // Get the observer object in target const ob = (target: any).__ob__ // If target is a vue instance or $data returns directly if (target._isVue || (ob && ob.vmCount)) { process.env.NODE_ENV !== 'production' && warn( 'Avoid adding reactive properties to a Vue instance or its root $data ' + 'at runtime - declare it upfront in the data option.' ) return val } // If ob does not exist, it means that target is not a responsive object, and the value is assigned directly without triggering a view update if (!ob) { target[key] = val return val } // If ob exists, set key as the responsive property defineReactive(ob.value, key, val) // Send notification to trigger view update ob.dep.notify() return val } The above is the source code of the set method in vue. It should be noted here that when processing an array, the splice method used is not the method of the array itself, but a responsive array method encapsulated in vue. This is the end of this article about the detailed source code case of Vue's vue.$set() method. For more relevant Vue's vue.$set() method source code content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: MySQL master-slave configuration study notes
This article shares the specific code for JavaScr...
The use of vue3 Teleport instant movement functio...
This article shares the specific code of js to ac...
1. Division of labor and process <br />At T...
Batch comments in SQL Server Batch Annotation Ctr...
Introduction The module that limits the number of...
<br />It has been no more than two years sin...
The problem that MYSQL5.7.17 cannot connect under...
Python connects to MySQL to modify and query data...
Recently, there is a requirement for uploading pi...
1. Install libfastcommon-1.0.43. The installation...
Using CI to build docker images for release has g...
This article example shares the specific code for...
Whitespace rules in HTML In HTML, multiple spaces...
Table of contents Preface Scope 1. What is scope?...