当元素没有id时如何处理vuejs中的v-for? [英] How to deal with v-for in vuejs when element has no id?

查看:136
本文介绍了当元素没有id时如何处理vuejs中的v-for?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

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

当使用这样的 for 循环时.

{{ person.name }} </div>

问题是,有时在极少数情况下,我没有人的 id 键.我想知道在这种情况下可以做什么.

解决方案

正如 Ghanshyam 在评论中已经提到的,你可以在 v-for 中轻松生成索引.

<div v-for="(person, index) in people" :key="index" >{{ 人名}}

但是,有一些事情要做表示 NOT 使用索引作为键.

其他 SO 帖子:为什么不总是使用索引作为 vue.js for 循环中的键?

I often get this error related to the v-for directive.

Elements in iteration expect to have v-bind:key directive

When using a for loop like this.

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

The problem is, in sometimes in rare cases, I have no id key for person. And I want to know what can be done in this case.

解决方案

As Ghanshyam already mentioned in comments, you can easily generate an index in 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.

Other SO Post: Why not always use the index as the key in a vue.js for loop?

这篇关于当元素没有id时如何处理vuejs中的v-for?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆