在VUE SFC内获取循环键
给定以下循环组件:
<MyComponent v-for="(item, i) in items" :key="i" :item="item" />
如何获得键
在myComponent
中的值显然key
是一个保留的单词,因此它无法作为一个支柱。
Given the following looping component:
<MyComponent v-for="(item, i) in items" :key="i" :item="item" />
How do I get the value of key
within MyComponent
obviously key
is a reserved word so it fails to work as a prop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其作为声明的支架传递(例如:
index
):mycomponent.vue:
side注意:建议您使用唯一标识符列出
键
lists(例如:id
id < /code>),而不是基于其在列表中的位置。例如,如果任何项目被更换或更新,VUE可能不会重新渲染,因为即使项目本身具有,这些项目在数组中的位置也没有更改。
这就是为什么您按索引关键项目时列表过渡不起作用的原因。
但是,如果您的项目不更改位置和/或不更新,则
index
作为key
执行工作。Pass it as a declared prop (e.g:
index
):MyComponent.vue:
Side note: it's recommended you
key
lists using a unique identifier (e.g:id
) rather than based on their position in the list. For example, if any items get replaced or updated, Vue might not re-render, because the items' position in the array has not changed, even if the item itself has.This is why list transitions don't work when you key items by index.
However, if your items don't change position and/or do not get updated,
index
askey
does the job.