文档说:
在 Vue 3.x 中,键应该放在标签上。
<template v-for="item in list" :key="item.id">
<div>...</div>
<span>...</span>
</template>
但我希望使用索引作为我的密钥,所以我这样做:
<template v-for="(item, index) in props.items" :key="index">
但我收到了警告index is declared but never read
。
怎么修?
键的要点是能够识别项目,无论它们位于列表中的位置。由于索引不是该项目固有的,因此它无法实现这一点。您只需不完全声明密钥即可达到与使用索引作为密钥相同的效果,但不建议这样做:https:
//vuejs.org/guide/essentials/list.html#maintaining-state-with-key