如下面发布的代码所示,我想添加一个
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>
你非常需要这样的东西
<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
或您的对象中任何独特的东西。
更多信息请参见文档。