Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue 为什么不要用 index 作为 key #86

Open
lovelmh13 opened this issue Jun 22, 2021 · 0 comments
Open

Vue 为什么不要用 index 作为 key #86

lovelmh13 opened this issue Jun 22, 2021 · 0 comments

Comments

@lovelmh13
Copy link
Owner

与 Diff 算法有关系,Diff 算法是同层比较,可以另外这个文章:lovelmh13/One-question-a-day#37

如果用了 index,我们调换数组的顺序: ['a', 'b', 'c'] -> ['c', 'b', 'a']

虽然数组的内容是变了,但是数组的 index,还是原来的 index,并不会内容变了,就改变索引, 索引依然是 0 1 2。所以写了跟没写一样,还是会复用元素。

所以在需要改变顺序的需求下,不要使用 index 作为 key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant