迭代中的元素期望有“v-bind:key”

问题描述 投票:0回答:1

如下面发布的代码所示,我想添加一个

drop down
列表。但 Visual Studio 代码用红色强调了
v-for
并生成以下错误:

Elements in iteration expect to have 'v-bind:key' directives

这是什么意思以及如何解决它?

代码

<select name="" id="" v-model="selected">
    <option value="" disabled>Select a car</option>
    <option v-for="car in cars">{{ car }}</option>
  </select>
vue.js vuejs3 vue-component
1个回答
0
投票

你非常需要这样的东西

<select name="" id="" v-model="selected">
  <option value="" disabled>Select a car</option>
  <option v-for="car in cars" :key="car.id">{{ car }}</option>
</select>

:key
可以有
car.id
car.name
或您的对象中任何独特的东西。

更多信息请参见文档

© www.soinside.com 2019 - 2024. All rights reserved.