我正在构建一个HackerNews的克隆,并收到以下错误:
vue.esm.js?efeb:591 [Vue warn]: Avoid using non-primitive value as key, use string/number value instead.
found in
---> <Single> at src/components/Single.vue
<App> at src/App.vue
<Root>
Run Code Online (Sandbox Code Playgroud)
该错误似乎来自Single.vue,但我无法正常工作?模板如下:
<template>
<div class="container">
<h2>{{ story.title }}</h2>
<p>Score: {{ story.score }}</p>
<p>{{ story.url }}</p>
<div v-for="comment in comments" :key="comment">
<div class="comment-wrap">
<div class="comment-block">
<p class="comment-text">{{ comment.text }}</p>
<div class="bottom-comment">
<div class="comment-author">{{ comment.by }}</div>
<div class="comment-date">{{ comment.time }}</div>
</div>
</div>
</div>
</div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮助您,那就太好了!
Wil*_*ong 14
警告从源<div v-for="comment in comments" :key="comment">,其中,所述对象comment被用作key对v-for。警告的含义很直白,请勿使用Object作为键。
使用唯一的原始值作为键,也许像comment.id或${comment.time}${comment.by}
小智 11
其他答案也可以,但是以下方法也值得一试:
<div v-for="(comment, idx) in comments" :key="idx">
<!-- ... -->
</div>
Run Code Online (Sandbox Code Playgroud)
小智 9
你可以简单地避免使用:key在你的v-for。
...
<div v-for="comment in comments">
...
Run Code Online (Sandbox Code Playgroud)
正如vuejs 文档所说:
建议尽可能使用v-for提供密钥,除非迭代的DOM内容很简单,或者您有意依赖默认行为来获得性能。
使用唯一的原始值作为键,建议尽可能提供一个key属性v-for。
喜欢
:key="design.uniqueId"
<li v-for="design in designs" :key="design.id">
<!-- code -->
</li>
<li v-for="loop in loops" :key="loop.id">
<!-- code -->
</li>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13556 次 |
| 最近记录: |