【发布时间】:2022-09-27 17:01:15
【问题描述】:
我在应用程序中使用 NuxtJS 和 Vuetify。当显示带有v-data-table 的记录表时,如果 item.isActive 为真,我想显示一个徽章。为此,我在<tr> 中循环遍历<template>。每当我提供<template> 标签的密钥时,我都会得到\'<template v-for>\' cannot be keyed. Place the key on real elements instead。当我尝试键入<td> 时,我得到<template v-for> key should be placed on the <template> tag. 这在带有ESLint 的VSCode 中面临
第一个错误
第二个错误
执行:
<v-data-table :items=\"items\">
<template v-slot:item=\"{ item }\">
<tr>
<template v-for=\"(header, index) in headers\">
<td v-if=\"header.value === \'isActive\'\" :key=\"index\">
<v-badge bordered content=\"\" offset-x=\"-10\" color=\"\'#64b54d\'\" />
</td>
<td
v-else
:key=\"index\"
>
{{ item[header.value] }}
</td>
</template>
</tr>
</template>
</v-data-table>
CodeSandbox 链接:https://codesandbox.io/s/runtime-wood-q4mc5h?file=/pages/home.vue:11-585
提前致谢。
-
我添加了一个答案。你有没有机会调查一下。希望它会按照您的期望工作。
-
eslint.vuejs.org/rules/no-v-for-template-key-on-child.html 这是您需要禁用的规则,即将
\"vue/no-v-for-template-key-on-child\": \"off\"放入 package.json 的eslintconfig>rules部分。但是,如果您的 VS Code 仍然在抱怨,那将是 VueJS 插件之一的工作 - 您可能需要与作者讨论它 -
请问你为什么在这里使用
template标签?为什么不在你的<tr>上加上 v-for? -
@miro 因为您不能在同一元素上使用 v-for 和 v-if 。
标签: vue.js nuxt.js vuetify.js