javascript - 控制台警告 : component lists rendered with v-for should have explicit keys

标签 javascript vue.js vuejs2

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

[Vue tip]: <todo-item v-for="todoItem in todos">: component lists rendered with v-for should have explicit keys. See https://v2.vuejs.org/v2/guide/list.html#key for more info.
(found in <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 建议的链接 here .我想我没有任何错误,我想解决那个警告,但我找不到原因,顺便说一句,我是 Vue 的新手。

最佳答案

答案在 documentation you linked 中明确列出...

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

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

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

关于javascript - 控制台警告 : component lists rendered with v-for should have explicit keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476942/

相关文章:

javascript - 在可内容编辑容器内的元素开头插入文本

javascript - VueJS vue-spinner 属性或方法 "loading"未定义

javascript - 作为 Vue 插件的一部分将 Mutations 添加到 Vuex store

vue.js - Vue.js 中的不同变量值 + 开发服务器与构建中的 Webpack

javascript - android中的自定义webview,水平滚动

javascript - JSON.parse(JSON.stringify(e)) 将对象转换为字符串

vue.js - Vue 3 为什么子数据更改时父级会更新?

javascript - 是否可以以同步模式执行 $emit 并从 emit 事件中获取结果

javascript - D3 : zoom to bounding box with d3-tiles

javascript - Vue-router嵌套路由未加载我的组件页面