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

[Vue tip]:<todo-item v-for="todoItem in todos">:使用v-for呈现的组件列表应该有显式键.更多信息请参见https://vuejs.org/v2/guide/list.html#key

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>

总结以下 comments 中的一些信息...使用:key让组件知道如何识别单个元素.这使它能够跟踪Vue reactivity的变化.

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

Vue.js相关问答推荐

我需要在 nuxt2 上使用 /index 作为 url 的一部分

作为props 传递的原始 ref 的 Vue 3 react 性

在 v-for 中定义变量有什么问题吗?

如何使用 Vite 从公共目录导入 JSON 文件?

props至少应该定义它们的类型

Webpack - MiniCssExtractPlugin 不提取文件

实现登录命令并访问 vuex 存储

未捕获的错误:[vue-composition-api] 必须在使用任何函数之前调用 Vue.use(plugin)

如何在 Vue.js 中使用 MaterializeCss?

如何像 React 中的 {...props} 一样在 Vue 中解构 props?

Axios-一次发出多个请求(vue.js)

如何从 bootstrap-vue 模态监听事件?

Vue更改宽度和内容

Modal 内部的 Vue.js AJAX 调用

阻止 Vue 积极重用 dom 元素

如何在 Vue3 设置标签中定义组件名称?

Vuetify Jest 未知自定义元素

未在实例上定义,但在渲染期间引用

Vue.js 单向绑定表单

在Vue中调用方法时的括号