Vue项目,使用v-for遍历变量,报错:component lists rendered with v-for should have explicit keys

本文详细解析了Vue项目中使用v-for遍历时遇到的常见错误:componentlistsrenderedwithv-forshouldhaveexplicitkeys,并提供了具体的解决方案。通过添加:key属性,可以避免此错误,确保列表渲染的正确性。

Vue项目使用v-for遍历变量,报错:component lists rendered with v-for should have explicit keys 的解决办法

操作环境

WebStorm + Vue + Element UI

出错的前端代码

<el-tag style="margin: 1px 2px;" 
	v-for="(r,indexj) in hr.roles"  
	type="success">{{r.namezh}}
</el-tag>

报错信息

(Emitted value instead of an instance of Error) :
component lists rendered with v-for should have explicit keys.
See https://vuejs.org/guide/list.html#key for more info.

原因和解决办法

原因

v-for 必须配置 :key

解决办法

把代码改成:

<el-tag style="margin: 1px 2px;" 
v-for="(r,indexj) in hr.roles" :key="indexj" 
type="success">{{r.namezh}}
</el-tag>

参考资料

为什么使用v-for时必须添加唯一的key?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值