vue中v-for写在template上,加key提示错误
v-for写在非template上,添加:key没有任何问题,但是写在template上就不行了,加了就报错
虽然不影响页面渲染,但终端一直报错显示,很讨厌;
有时候,页面渲染,还是需要不加div层的基础上渲染的,vscode编辑器有的也有错误红线提示;
'<template>' cannot be keyed. Place the key on real elements instead.
<template v-for="(item,i) in functionList" :key="i">
<div >{{item.label}}</div>
</template>
原因:不支持在 template 元素上绑定属性。比如这里想绑定 key 属性就不行。
解决办法:
<template v-for="(item,i) in functionList">
<div :key="i">{{item.label}}</div>
</template>
相信坚持的力量,日复一日的习惯.