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

JavaScript

乐理查德

2020-03-11

我在这里遇到问题,我不知道我的代码有什么问题,但是在控制台中收到警告,如何删除此警告?

[提示]::<todo-item v-for="todoItem in todos">用v-for呈现的组件列表应具有显式键。有关更多信息,请参见https://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

在此处输入图片说明

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

第531篇《控制台警告:使用v-for呈现的组件列表应具有显式键》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
路易Sam神无 2020.03.11

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

- <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"> ...
Mandy小卤蛋凯 2020.03.11

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

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

总结以下注释中的一些信息...,您:key可以使组件知道如何标识各个元素。这样就可以跟踪Vue 反应性的变化

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android