What is the function of this key attribute? Let’s first look at the official explanation:
Let me briefly mention my understanding of VNode:
For example: <div class="title" style="color: red;">Helllo World</div> // Actually in Vue it will be represented as: const VNode = { type: "div", props: { class: "title", style: { color: "red" } }, children: "Hello World" // If the div contains other tags, they will be converted to children in the same way} Under what circumstances is the insertion of f most efficient? Ideas:
Conclusion: If you want the highest performance, you must key the new and old VNodes, compare the old VNodes with the new VNodes by key value, and find out which VNode needs to be added or deleted. Other VNodes should remain unchanged as much as possible. The process of comparing the new and old VNodes is the diff algorithm. Vue actually calls two different methods for keys and no keys. Let's take a look at the source code: (packages\runtime-core\src\renderer) In the case of a key, execute the patchKeyedChildren method: If there is no key value, execute the patchUnkeyedChildren method: Note: When there is no key value, if there are too many lists, patches will be performed one by one, which will consume a lot of performance. Then adding the key value will save a lot of performance loss. in conclusion: Therefore, the key value plays a role when the DOM tree performs the diff algorithm. One function is to determine whether the new and old Vnodes are the same, so as to perform the next step of comparison and rendering. The other function is to determine whether the component can be reused and whether it needs to be re-rendered. This is the end of this article about the role and working principle of key in Vue3. For more information about the role and working principle of key in Vue3, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Two ways to manually implement MySQL dual-machine hot standby on Alibaba Cloud Server
>>: How to use Linux tr command
grammar background: linear-gradient(direction,col...
Record the installation of two MySQL5.6.35 databa...
1. Introduction Sometimes, after the web platform...
What is Redis Cluster Redis cluster is a distribu...
This article example shares the specific code of ...
1. Check whether MySQL is installed yum list inst...
First time writing. Allow me to introduce myself....
Solution 1 Completely uninstall and delete all da...
In our recent project, we need to use Google robo...
The link-in style is to put all the styles in one...
Table of contents 2. Tried methods 2.1 keep-alive...
Table of contents Preface - Vue Routing 1. The mo...
HTML form tag tutorial, this section mainly expla...
Table of contents Problem description: Solution 1...
Version Chain In InnoDB engine tables, there are ...