组件渲染功能中可能有无限的更新循环

node.js Node.js

十三Davaid

2020-03-12

我是VueJS的新手,已经收到Vue的警告,

[Vue warn]: You may have an infinite update loop in a component render function. 

当我在V-bind:style中使用V-for变量时,这是一个示例:在template中:

<div v-for="item in model.items" v-bind:class="test(item.result)">
{{item.id}}
</div>

在脚本中:

data() {
    return {
        accept: false,
        not_accept: false,
    };
},
methods: {
    test(result) {
        if (result == 'accept') {
            this.accept = true;
            this.not_accept = false;
        } else if (result == 'Not accept') {
            this.accept = false;
            this.not_accept = true;
        } else {
            console.log(result);
        }

        return {
            success: this.accept,
            danger: this.not_accept,
        };
    },
},

第1148篇《组件渲染功能中可能有无限的更新循环》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
猴子村村 2020.03.12

首先,我不确定您为什么拥有它not_accept,您不能只使用!this.accept它吗?

我不确定100%为什么收到此警告,但这就是我的想法。

对于观察者v-bind:class是看的变化item.resultthis.acceptthis.not_accept这些值的任何更改都将导致通过test再次调用重新呈现它但中test,你正在修改this.acceptthis.not_accept,所以Vue公司需要重新若结果的,因为那改变,这样做可能会改变再检查this.acceptthis.not_accept 等。

class绑定和数据是有缺陷的。class每个项目都将设置为同一事物,但看起来您似乎希望根据来为每个项目自定义样式item.result您确实不应该修改thisinside的任何属性test

很难给出全面的答案,因为我不确定您的组件如何工作以及应该做什么。

神奇西里泡芙 2020.03.12

如果调用一个函数而不是在vue指令中传递一个函数,则会出现此错误。这是一个例子:

我显示了一个自定义指令,以在显示boostrap选项卡时通过AJAX加载数据

这是不好的:

 v-on-show-bs-tab="getFirstPageSites()"

在这里,vue似乎调用了该​​函数(或更确切地说是对表达式求值)并将结果传递给指令。

这很好:

 v-on-show-bs-tab="getFirstPageSites"

在这里,我通过名称传递函数,以便可以在指令中调用它。

问题类别

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