vue使用v-for时vscode报错Elements in iteration expect to have 'v-bind:key' directives

Vue2.2.0+的版本里,当在组件中使用v-for时,key是必须的

错误提示:

1
2
[vue-language-server] Elements in iteration expect to have 'v-bind:key' directives.
Renders the element or template block multiple times based on the source data

原因是eslint检测引起

解决方法:

  1. 在v-for后添加:key=‘item’

    1
    2
    3
    <li v-for="i in list" :key="i">

    <div class="item" v-for="(user,index) in datalist" :key='index' >
  2. 在build处关闭eslint检测
    …(config.dev.useEslint ? [createLintingRule()] : []),

  3. 更改vetur配置:vscode->首选项->设置->搜索(vetur)
    “vetur.validation.template”: true,改成:false

参考链接:

vue使用v-for时vscode报错 Elements in iteration expect to have ‘v-bind:key’ directives