当前位置:网站首页>v-for中的key的作用和原理
v-for中的key的作用和原理
2022-06-11 17:55:00 【湛海不过深蓝】
key是虚拟dom对象的唯一标识。
v-for循环时,根据数据生成虚拟dom,在根据虚拟dom生成真实dom。
当数据发生变化时,会生成新的虚拟dom。
然后vue会将新的虚拟dom和初始虚拟dom进行对比,即:diff算法
如果新的虚拟dom中的key和初始虚拟dom中的key一致:
- 如果虚拟dom的内容没变,就直接复用之前的真实dom
- 如果虚拟dom的内容变了,就生成新的dom节点,替换掉之前的
如果新的虚拟dom中的key和初始虚拟dom中的key不一致:
- 就创建新的真实dom节点,随后渲染页面
用index作为key值可能会影发的问题:
- 当对数据进行逆向删除或者添加操作的时候,会产生没有必要的真实dom节点的更新,或者产生错误的dom更新,直接导致界面错误。
边栏推荐
- 光纤熔接知识汇总【转载自微信公众号弱电智能化工程2018】
- [not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration
- 剑指 Offer(第 2 版)
- Can 400 fans earn 20W a month? How did you do it?
- Hash table, inheritance
- 6-6 batch sum (*)
- "College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
- 7-2 h0107. Pig-Latin
- Common shortcut keys for Hello go (x) and GoLand
- Learning about canvas API
猜你喜欢

送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔

【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
![[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)

NR LDPC 打孔-punctured
![[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (I)](/img/3e/efce1cff81abf395ffc24ac57b05d7.png)
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (I)

【先收藏,早晚用得到】100个Flink高频面试题系列(三)

Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall

Common shortcut keys for Hello go (x) and GoLand

ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”

Bracket generation ---2022/02/25
随机推荐
Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
Hello go (XIV). Go language common standard library IV
【实用脚本】获取某个文件的行号,然后删除文件内容。
[pat grade B question bank] complete summary
Winter vacation daily question (improvement group) [end of week 4]
【先收藏,早晚用得到】100个Flink高频面试题系列(二)
Install MariaDB 10.5.7 (tar package installation)
NFT platform development NFT mall source code NFT mall development chain game development
7-2 h0107. Pig-Latin
Bracket generation ---2022/02/25
LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
安全领域常规术语
How to learn and self-study
vulhub
How ZABBIX can customize MySQL monitoring items and trigger alarms
密码学概述
Hello go (XI). Go language common standard library I
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (I)
Dynamic: capturing network dynamics using dynamic graph representation learning
Getting started with CTF