简体   繁体   English

Vue.js 警告你可能在组件渲染函数中有无限更新循环

[英]Vue.js warns You may have an infinite update loop in a component render function

I'm quite new to Vue.js & Buefy and I believe I understand why this happens but have no idea how to fix it.我对 Vue.js 和 Buefy 很陌生,我相信我明白为什么会发生这种情况,但不知道如何解决。

I have a list of project partners sorted by country and I need to output a list with checkboxes (I'm using Buefy on the page anyway) and country names as title (only when there's a "new" country).我有一个按国家/地区排序的项目合作伙伴列表,我需要输出一个带有复选框的列表(无论如何我在页面上使用 Buefy)和国家/地区名称作为标题(仅当有“新”国家/地区时)。 This ends with browser doing infinite loop (verified with console.log) and Vue Devtools gives warning "You may have an infinite update loop in a component render function".这以浏览器执行无限循环(通过 console.log 验证)结束,Vue Devtools 给出警告“您可能在组件渲染函数中有无限更新循环”。

I believe this happens because changing prevTitle triggers re-rendering.我相信这是因为改变 prevTitle 会触发重新渲染。 I know it's not possible to pass parameters to computed properties and I haven't been able to use any of the tricks I've found to make partner.country available there.我知道不可能将参数传递给计算属性,而且我无法使用我发现的任何技巧来使 partner.country 可用。

var app = new Vue({
    el: "#app",
    data: {
      prevTitle: ""
    ...

    methods: {
      changeCountryTitle(country) {
        if (country != this.prevTitle) {
          this.prevTitle = country;
          return true;
        }
        return false;
    }
<template v-for="partner in results">
    <div v-if="changeCountryTitle(partner.country)">
        {{ partner.country }}
    </div>
    <b-checkbox ... >{{ partner.name }}, ...</b-checkbox>
    <br />
</template>

Then I tried to make a computed property where I do all processing instead of for loop in template and return a string that contains everything, including Buefy tags, which would be called然后我尝试创建一个计算属性,我在模板中进行所有处理而不是 for 循环,并返回一个包含所有内容的字符串,包括 Buefy 标签,它将被调用

<span v-html="printPartnerList"></span>

But those Buefy tags don't get rendered properly, only HTML tags work, browser ignores Buefy tags showing the normal text only.但是那些 Buefy 标签无法正确呈现,只有 HTML 标签有效,浏览器会忽略仅显示普通文本的 Buefy 标签。

Any ideas how get this working?任何想法如何让这个工作? For time being I print country names for each partner after name etc. but it's not how this is supposed to work.目前,我在名称等之后为每个合作伙伴打印国家/地区名称,但这不是应该如何工作。

v-html doesn't evaluate Vue (or in this case Buefy) components, only regular HTML tags. v-html不评估 Vue(或在本例中为 Buefy)组件,仅评估常规 HTML 标签。

Your first approach wasn't that bad, but instead of calling a method inside the v-for you can add a computed property in the items that tells if the country should be rendered:您的第一种方法并没有那么糟糕,但是您可以在项目中添加一个计算属性,而不是在v-for中调用一个方法,该属性告诉您是否应该呈现国家/地区:

<template v-for="partner in computedResults">
    <div v-if="partner.showCountry">
        {{ partner.country }}
    </div>
    <b-checkbox ... >{{ partner.name }}, ...</b-checkbox>
    <br />
</template>

New app:新应用:

var app = new Vue({
    el: "#app",
    data: {
      // prevTitle: "" <- not needed
    ...

    computed: {
      computedResults() {
        let prevCountry = ''
        let newResults = []
        this.results.forEach(partner => {
          let showCountry = false
          if (prevCountry != partner.country) {
            showCountry = true
          }
          newResults.push({
            ...partner,
            showCountry
          })
          prevCountry = partner.country
        })
        return newResults
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue组件中的“组件渲染函数中可能存在无限更新循环”警告 - “You may have an infinite update loop in a component render function” warning in Vue component Vue 道具更新 - 您可能在组件渲染函数中有无限更新循环 - Vue props update - You may have an infinite update loop in a component render function 如何修复或抑制误报“组件渲染功能中可能存在无限更新循环” Vue警告 - How to fix or suppress a false positive “You may have an infinite update loop in a component render function” Vue warning 如何修复 Vue 警告:组件渲染函数中可能存在无限更新循环 - How to Fix Vue Warning: You may have an infinite update loop in a component render function 您可能在组件渲染函数中有一个无限的更新循环? - You may have an infinite update loop in a component render function? 如何避免 [Vue 警告]:您可能在组件渲染 function 中有无限更新循环。 在 VUEJS 中 - How can I avoid [Vue warn]: You may have an infinite update loop in a component render function. in VUEJS 即使使用扩展运算符,您也可能在组件渲染 function 中有无限更新循环 - You may have an infinite update loop in a component render function even with spread operator 即使使用扩展运算符,您也可能在组件渲染函数中存在无限更新循环,但 Object.assign 运行良好 - You may have an infinite update loop in a component render function even with spread operator, but Object.assign works well Vue触发器中的简单切换功能:您可能有无限的更新循环 - Simple toggle function in Vue trigger: You may have an infinite update loop 当我添加这行代码时“您可能在组件渲染函数中有无限更新循环” - console.log(perksTree.slots.unshift()) - “You may have an infinite update loop in a component render function” when I add this line of code - console.log(perksTree.slots.unshift())
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM