当前位置:网站首页>Data disorder occurs when the n-th row of the subcomponent list generated by V-for is deleted
Data disorder occurs when the n-th row of the subcomponent list generated by V-for is deleted
2022-06-13 07:45:00 【Niufenfen】
As shown in the following figure

After clicking the delete button , Data disorder occurs

Consult official documents , Found that when Vue Updating usage v-for When rendering a list of elements , It uses by default “ in-place update ” The strategy of . If the order of data items is changed ,Vue Will not move DOM Element to match the order of data items , Instead, each element is updated in place , And make sure they render correctly at each index location .
This default mode is efficient , however Only applicable to non dependent sub component status or temporary DOM state ( for example : Form input value ) List render output for .
To give Vue A hint , So that it can track the identity of each node , To reuse and reorder existing elements , You need to provide a unique key attribute:
Look at the code ,v-for Generated components , The binding is key May not be the only value , Can't bind index

So I gave it to v-for Component's key Bind a getTime(), Think of this every key The value will not be repeated , And it's the only one

After testing , Found deleted rows , The page data disorder is solved , But other problems arise , The cascade selector cannot be selected for the first time , The second time you can , After debugging, it is found that each operation of the cascade selector ,@change Bound functions console Will print twice . Add row each time , The data in the previous row will be empty , I can't figure it out , Another big circle ,
‘


collapsed 10 Minutes later , I went to check the official explanation ,key Special attribute Mainly used in Vue The virtual DOM Algorithm , New and old nodes Identification in contrast VNodes. If not used key,Vue Will use a way to minimize dynamic elements and try to modify them in place as much as possible / Algorithms for reusing elements of the same type . While using key when , It will be based on key Changes to reorder elements , And it will be removed key Elements that do not exist .
Child elements with the same parent must have Distinctive key. Repetitive key Can cause rendering errors .
Found directly in dom It says in it getTime(), Of each generated component key It's all dynamic , Although every key It's all unique , But like this key It's meaningless , Every time key Changed , The element will be re rendered , So every time you add a row , The previous data is also empty , Every operation dom, All generate new , Instead of taking the old DOM To update the location , This will cause performance problems .
After modification, a unique... Is generated in the data key, And then in dom The binding , So there is no problem
![]()
link
边栏推荐
- 20 | 面向流水线的指令设计(上):一心多用的现代CPU
- P6154 游走(记忆化搜索
- v-for生成的子组件列表删除第n行出现数据错乱问题
- 18 | establish data path (middle): instruction + operation =cpu
- Advanced MySQL: how to learn MySQL source code
- MySQL row column conversion (updated version)
- String source code analysis
- 论文笔记: 多标签学习 BP-MLL
- About database: pgadmin4 editing SQL window
- Redis learning journey sentinel mode
猜你喜欢

Precautions for passing parameters with byte array

First graphical interface

21 | pipeline oriented instruction design (Part 2): How did Pentium 4 fail?

比较DFS和BFS的优点和缺点及名称词汇

How idea breaks point debugging

安装CUDA+CUSP环境,并创建第一个HelloWord入门工程

26 | Superscalar和VLIW:如何让CPU的吞吐率超过1

C语言:如何给全局变量起一个别名?

Paper notes: multi label learning bp-mll

Redis learning journey -- subscription and publishing
随机推荐
[log framework] add user-defined parameters to the MDC implementation log
26 | Superscalar和VLIW:如何让CPU的吞吐率超过1
2021-10-20
The management practice of leading enterprises has proved that what is the core of sustainable development of enterprises?
11.29 Li Kou swipes questions every day
5. interrupts and exceptions
A. Vacations (DP greed
22 | 冒险和预测(一):hazard是“危”也是“机”
Redis Cluster - the bottom principle of building clusters
[log4j2 log framework] sensitive character filtering
19 | establish data path (bottom): instruction + operation =cpu
Nodejs file module FS
EF core execute SQL statement
Redis master-slave replication - mentality detection mechanism
【深度学习】:《PyTorch入门到项目实战》(十二)卷积神经网络:填充(padding)和步幅(stride)
How app inventor accesses resource files in assets directory
Distributed transaction learning (I) preliminary understanding
Shell script Basics
【Emgu.CV】Emgu. CV. Example\ocr operation reports an error system IO. Filenotfoundexception: "failed to load file or assembly" system.drawing.common "
Redis cluster parsing docker building redis cluster
https://cn.vuejs.org/v2/guide/list.html#%E5%8F%98%E6%9B%B4%E6%96%B9%E6%B3%95