Javascript 控制台警告:使用 v-for 呈现的组件列表应具有显式键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42476942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:10:36  来源:igfitidea点击:

Console warning: component lists rendered with v-for should have explicit keys

javascriptvue.jsvuejs2

提问by John Rey M. Baylen

I got a problem here, I don't know what is wrong in my code, but I got a warning in my console, how can I remove this warning?

我在这里遇到了一个问题,我不知道我的代码有什么问题,但是我的控制台中出现了一个警告,我该如何删除这个警告?

[Vue tip]: <todo-item v-for="todoItem in todos">: component lists rendered with v-for should have explicit keys. See https://vuejs.org/v2/guide/list.html#keyfor more info.
(found in <Root>)

[Vue 提示]: <todo-item v-for="todoItem in todos">: 用 v-for 渲染的组件列表应该有明确的键。有关更多信息,请参阅https://vuejs.org/v2/guide/list.html#key
(在 中找到<Root>

index.html

索引.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Vue Tutorial</title>
        <link rel="shortcut icon" href="https://vuejs.org/images/logo.png">
        <script src="scripts/vue.js"></script>
    </head>
    <body>
        <section id="app">
            <p>{{ msg }}</p>
            <p v-bind:title="message">
                Hover your mouse over me for a few seconds to see my dynamically bound title!
            </p>
            <div>
                <p v-if="seen">This text will show or hide if the button was clicked.</p>
                <button type="button" v-on:click="isSeen">{{ isSeenText }}</button>
            </div>
            <ol>
                <li v-for="todo in todos">
                    {{ todo.text }}
                </li>
            </ol>
            <p>Total count: {{ todos.length }}</p>
            <div v-bind:title="reverseMessageText">
                <p>{{ reverseMessageText }}</p>
                <button v-on:click="reverseMessage">Reverse Message</button>
            </div>
            <div>
                <p>Data binding: <strong>{{ nameOfUser }}</strong></p>
                <input type="text" v-model="nameOfUser">
            </div>
            <div>
                <ol>
                    <todo-item v-for="todoItem in todos" v-bind:data="todoItem"></todo-item>
                </ol>
            </div>
        </section>
        <script src="scripts/app.js"></script>
    </body>
</html>

app.js

应用程序.js

var appComponent = Vue.component('todo-item', {
    template: '<li>id: {{ data.id }}<br>text: {{ data.text }}</li>',
    props: [
        'data'
    ]
});

new Vue({
    el: '#app',
    data: {
        msg: 'Hello World!',
        message: 'You loaded this page on ' + new Date(),
        seen: true,
        isSeenText: 'Now you don\'t',
        todos: [
            {
                text: 'Learn JavaScript'
            },
            {
                text: 'Learn Vue'
            },
            {
                text: 'Build something awesome'
            }
        ],
        reverseMessageText: 'Hello World from Vue.js!',
        nameOfUser: 'John Rey'
    },
    methods: {
        reverseMessage: function() {
            this.reverseMessageText = this.reverseMessageText.split('').reverse().join('');
        },
        isSeen: function() {
            this.seen = !this.seen;
            this.isSeenText = this.seen ? 'Now you don\'t' : 'Now you see me';
        }
    }
});


console.log

enter image description here

在此处输入图片说明

Here is the link that Vue suggested here. I think i don't have any error, I want to solve that warning but I cannot find where's the cause, btw I'm newbie here to Vue.

这是 Vue在此处建议的链接。我想我没有任何错误,我想解决该警告,但我找不到原因,顺便说一句,我是 Vue 的新手。

回答by Phil

The answer is listed explicitly in the documentation you linked...

答案在您链接文档中明确列出...

<todo-item v-for="todoItem in todos"
           v-bind:data="todoItem"
           v-bind:key="todoItem.text"></todo-item>


To summarise some information from the comments below... you use :keyto let the component know how to identify individual elements. This allows it to keep track of changes for Vue's reactivity.

从下面的评论中总结一些信息......您:key用来让组件知道如何识别单个元素。这允许它跟踪 Vue反应性的变化。

It's best to try and bind the :keyto some uniquely identifying property of each item. For example, an id.

最好尝试将 绑定:key到每个项目的某些唯一标识属性。例如,一个id.

回答by Henry

My solution to a similar problem looked like this:

我对类似问题的解决方案如下所示:

- <el-radio v-for="option in field.options"> ...
+ <el-radio v-for="(option, index) in field.options" :key="index"> ...

Or using v-bindsyntax for index:

或使用v-bind语法index

+ <el-radio v-for="(option, index) in field.options" v-bind:key="index"> ...

回答by Ioannis Chrysochos

You can use any field of your data as a key. In addition you can use the default id. Furthermore you can define a "key" in your data as in the code below:

您可以使用数据的任何字段作为键。此外,您可以使用默认 ID。此外,您可以在您的数据中定义一个“键”,如下面的代码所示:

Vue.component('task-list', {
template:  `
<div><slot>
    <task v-for="task in tasks" :key="task.key">  {{task.description}}</task>
</slot></div>
`,
data () {
    return {
        tasks: [
                {description:"Go to market", completed:false, key:"asd"},
                {description:"Wake up ", completed:true, key:"rty"},
                {description:"Sleep", completed:false, key:"terw"},
                {description:"Have breakfast", completed:true, key:"jdr"},
        ]
    };
},
});
Vue.component('task', {
   template: `<li><slot></slot></li>`
});

In the place of the key in the task.key you can put one of the field names including the hidden id.

在 task.key 中键的位置,您可以放置​​字段名称之一,包括隐藏的 id。