在學習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相當於是索引的作用,提高循環性能