Home>Article>Web Front-end> An article discusses the difference between key in Vue2 and key in Vue3

An article discusses the difference between key in Vue2 and key in Vue3

青灯夜游
青灯夜游 forward
2023-04-26 17:41:42 1331browse

As aVuedeveloper, we all know that when using thev-fordirective to render a list inVue, it must be in each child component. Add akeyattribute. Thiskeyattribute is a special attribute used to identify the uniqueness of each node. There is a big difference between thekeyin theVue2.xversion and thekeyin theVue3.xversion, so in this blog , we will discuss the difference betweenkeyinVue2andkeyinVue3.

An article discusses the difference between key in Vue2 and key in Vue3

key in Vue2

In theVue2.xversion,keyMainly used for optimization strategies inVue’s virtual DOM algorithm. In Vue2, when using thev-forinstruction to render a list,Vuewill compare the old and new nodes based on the value ofkey, and thenDOMUpdate.keyinVue2has the following characteristics:

  • key must be a string or numeric type,cannot be an object and array. [Related recommendations:vuejs video tutorial,web front-end development]

  • ##The value of key must be unique and cannot be repeated.
  • The value of key must be predictable and
  • cannot be

    randomly generated.

  • In
Vue2

, due to the use of thekeyoptimization strategy,DOMrearrangement and Re-rendering improves rendering performance and avoids erroneous data updates.

key in Vue3

In the

Vue3.x

version, the role ofkeyis the same asVue2.x# Thein the ## version is different, it is mainly used totrack the identity of the node. InVue3, when using thev-fordirective to render a list,Vuewill determine which nodes are new based on the value ofkeyWhich nodes were added and deleted, and then the DOM was updated.keyinVue3has the following characteristics:

key can be of any type,
    including
  • objects and arrays.

    The value of key must be unique and cannot be repeated.
  • The value of key
  • can
  • be non-predictable, such as a randomly generated value. The difference between

    keyin

Vue3andVue2is that the key in Vue3 is used for Keep track of node identity, rather than just optimizing rendering. This means that inVue3, theuniqueness ofkeymust be guaranteed, otherwise it will lead to node identity confusion, resulting in incorrect rendering results.This design is mainly toimprove rendering efficiency and performance

. In

Vue2, there may be some problems when dealing with dynamic lists.Thereason

for this problem is because

Vue2just makes asimple judgment based on thekeyvalue, and then compares The difference between old and new nodes makes it impossible to accurately know which nodes are added and which nodes are deleted.

Vue3An article discusses the difference between key in Vue2 and key in Vue3solves these problems. Using

key

you canaccurately determinewhich nodes are added and which nodes are deleted. This makesVue3more efficient and accurate when processing dynamic lists, avoiding some problems existing inVue2. Therefore, designingkeyto track the identity of nodes is an important optimization forVue3in improving rendering efficiency and performance.Conclusion

key

in Vue2

andkey# inVue3##There is a big difference in usage. ThekeyinVue2is mainly used to optimize rendering performance, while thekeyinVue3is mainly used to track the identity of the node. ThekeyinVue3can be of any type, including objects and arrays, but uniqueness must be guaranteed.Whether we are usingVue2orVue3

, we need to note that

key must be added when using thev-forinstruction to render the listAttribute, this is to ensure the correctness of rendering.(Learning video sharing:vuejs introductory tutorial,Basic programming video

)

The above is the detailed content of An article discusses the difference between key in Vue2 and key in Vue3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete