控制台警告:使用v-for渲染的组件列表应该有明确的key值。

81
我遇到了一个问题,我的代码出现了警告,但我不知道是什么问题,该如何去除这个警告?
【Vue提示】:<todo-item v-for="todoItem in todos">:使用v-for渲染组件列表时应该加上明确的键。更多信息请参见https://v2.vuejs.org/v2/guide/list.html#key。(位于<Root>中)

index.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

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

这里是Vue建议的链接这里。我认为我没有任何错误,我想解决那个警告,但我找不到原因,顺便说一句,我是一个Vue的新手。

3个回答

118

你链接的文档明确列出了答案...

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

总结下面的评论内容...你可以使用:key来让组件知道如何识别每个元素。这使得Vue在响应式方面可以追踪到变化。

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


1
在我的情况下,我希望整个待办事项对象被传递到<todo-item>,而不仅仅是传递.text。这可能吗? - Kokodoko
@kokodoko 整个项目通过 data 属性传递。您可以使用 key 来让组件知道如何识别单个元素。 - Phil
1
好的,谢谢。所以键只是Vue跟踪事物的一种方式,因此您需要有一些值来用作键。 - Kokodoko
1
@Kokodoko 是的,完全正确。 - Phil

14

我对类似问题的解决方案是这样的:

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

或者使用v-bind语法绑定index

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

这是一个很好的解决方案。我的元素并不是真正独特的,所以这个方案可以解决这个问题。 - khan
1
@khan,除非您的“选项”列表是静态的,否则这对于OP的问题来说是一个非常糟糕的解决方案。由于呈现的项目将通过它们在列表中的位置进行标识,任何重新排序该列表的操作都会带来难以发现的错误。 - Samuel Åslund

3
您可以使用数据中的任何字段作为键。此外,您还可以使用默认的 id。此外,您可以在您的数据中定义一个“key”,示例代码如下:
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>`
});

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接