【问题标题】:How to deal with v-for in vuejs when element has no id?当元素没有id时如何处理vuejs中的v-for?
【发布时间】:2017-10-04 12:27:39
【问题描述】:

我经常收到与v-for 指令相关的错误。

迭代中的元素期望有v-bind:key 指令

当使用这样的 for 循环时。

<div v-for='person in people'> {{ person.name }} </div>

问题是,在极少数情况下,我没有id 个人密钥。我想知道在这种情况下可以做什么。

【问题讨论】:

  • 如果没有ID,给key添加索引值
  • 你的意思是:&lt;div v-for='(person, index) in people'&gt;?
  • 是的,像这样给键添加索引
  • &lt;div v-for='(person, index) in people' :key="index"&gt;&lt;/div&gt;

标签: vuejs2


【解决方案1】:

正如 Ghanshyam 在 cmets 中已经提到的,您可以轻松地在 v-for 中生成索引。

<div v-for="(person, index) in people" :key="index" >
    {{ person.name }}
</div>

However, there is something to be said for NOT using index as a key.

其他 SO 帖子:Why not always use the index as the key in a vue.js for loop?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2020-08-19
    • 2019-07-11
    • 2019-05-09
    • 2017-06-03
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多