Elements in iteration expect to have 'v-bind:key' directives.' 提示错误如何解决?

在学习vue过程中遇到Elements in iteration expect to have 'v-bind:key' directives.' 这个错误,查阅资料得知Vue 2.2.0+的版本里,当在组件中使用v-for时,key是必须设置的。

解决方式一:设置对应的key

注意上面key值不要用对象或是数组作为key,用string或number作为key,否则报错:[Vue warn] Avoid using non-primitive value as key, use string/number value instead.

解决方式二:更改VS Code编辑器的vetur配置(vscode->文件->首选项->设置->搜索(vetur))

   将"vetur.validation.template": true, 改成"vetur.validation.template": false, 即可

两种方式对比,建议还是用第一种方式,:key相当于是索引的作用,提高循环性能

原文地址:https://www.cnblogs.com/changxue/p/8521140.html