【问题标题】:Vuejs infinite loopVuejs无限循环
【发布时间】:2020-11-18 18:04:46
【问题描述】:

这是我的代码,vue 警告: menu.js:39016 [Vue 警告]:您可能在组件渲染函数中有无限更新循环。 我不知道为什么,请帮助我。我在循环中使用 v-for 时有这个

<template>
    <div>
        <table class="table">
            <thead class="thead-dark">
                <tr>
                <th scope="col">#</th>
                <th scope="col">Name</th>
                <th scope="col">Image</th>
                <th scope="col">Category</th>
                <th scope="col">Additional Information</th>
                <th scope="col">Created By</th>
                <th scope="col">Action</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="menu in menuData" :key="menu.id">
                <th scope="row">{{ stt++ }}</th>
                <td>{{ menu.name }}</td>
                <td>{{ menu.image_url }}</td>
                <td><ul v-for="category in menu.categories" :key="category.id"><li>{{ category.name }}</li></ul></td>
                <td>{{ menu.additional_information }}</td>
                <td>{{ menu.menu_user.name }}</td>
                <td></td>
                </tr>
            </tbody>
        </table>
    </div>
    </template>
    
    <script>
        export default {
            mounted() {
                console.log('Component mounted.')
            },
            data: function() {
                return {
                    stt: 0,
                    menuData: [],
                }
            },
            created: function() {
                axios.get('/api/menus').then((response) => {
                    this.menuData = response.data.data.data;
                    console.log(this.menuData);
                });
            }
        }
    </script>

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    这是因为您直接在template 中更改数据导致重新渲染。

    <th scope="row">{{ stt++ }}</th>
    

    不要改变模板中的数据。

    正如@rjcarl 提到的,您可以在v-for 中获取行索引

    <tr v-for="(menu, index) in menuData" :key="menu.id">
    <th scope="row">{{ index }}</th>
    

    【讨论】:

    • 使用index 或数据的id 来显示行索引。 (menu, index) in menuData
    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2015-06-25
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多