Vue中使用v-for时的无限循环问题

代码如下,在v-for里面使用了计算属性的方法,对props数据进行了更改,数据更改以后会造成页面重新渲染会再次触发v-for就会造成死循环。

<template>
  <div class='growth'>
    <div class='monthGrowth'>
      <template
        v-for="(item,index) in progressStyleOrder"
      >//这里progressStyleOrder是在computed里面进行处理后的数据然后再渲染
        <Process
          :key="index"
          class="process"
          :width="item.width"  
          :height="0.6"
          contentBgColor="transparent" 
          :progressbgColor="item.color"
          :showDesc="true"
          :desc="item.desc"
          textPosition = "flex-end"
        ></Process>
      </template>
      <div class="label" :style="{background:progressStyleOrder[0].color,progressStyleOrder[0].width-0.4+'rem'}">
          <span>增长{{progress.growthRate}}%</span>
          <div class='triangle' :style="{progressStyleOrder[0].color}"></div>
      </div>
    </div> 
  </div>
</template>
<script>
    import { Process } from 'ftk-mui'
    export default {
      name: 'growth',
      components:{
      },
      props: [
        "progress"
      ],
      data(){
        return {
         
        }  
      },
      created(){
        
      },
      mounted(){
        
      },
      methods:{
        
      },
      computed:{
//这里是v-for内使用的数据
        progressStyleOrder: function () {
          return this.progress.progressStyle.concat().sort(function (a,b) {
            return b['width'] - a['width'];
          });
        }
      }
    }
</script>

解决办法,在mounted以后进行数据处理,得到新的数据,然后再挂载在dom上

<template>
  <div class='growth'>
    <div class='monthGrowth'>
      <template
        v-for="(item,index) in progressOrderList"
      >
        <Process
          :key="index"
          class="process"
          :width="item.width"  
          :height="0.6"
          contentBgColor="transparent" 
          :progressbgColor="item.color"
          :showDesc="true"
          :desc="item.desc"
          textPosition = "flex-end"
        ></Process>
      </template>
      <div class="label" :style="{background:progressOrderList[0].color,left:progressOrderList[0].width-0.4+'rem'}">
          <span>增长{{progress.growthRate}}%</span>
          <div class='triangle' :style="{borderTopColor:progressOrderList[0].color}"></div>
      </div>
    </div> 
  </div>
</template>
<script>
    import { Process } from 'ftk-mui'
    export default {
      name: 'growth',
      components:{
      },
      props: [
        "progress"
      ],
      data(){
        return {
          progressOrderList:[],
        }  
      },
      created(){
        
      },
      mounted(){
        this.progressStyleOrder();
        this.progressStyleOrderReverse();
      },
      methods:{
//将方法在methods中定义,并且将新的数据存储在data中,然后在mounted后触发
        progressStyleOrder: function () {
          let progressOrder = this.progress.progressStyle.concat().sort(function (a,b) {
            return b['width'] - a['width'];
          });
          this.progressOrderList = progressOrder
        },
      },
      computed:{
        
      }
    }
</script>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vue概述sd 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来...
    去年的牛肉阅读 9,538评论 0 1
  • vue.js是什么 是一套构建用户界面的渐进式框架 vue应用组成 一个 Vue 应用由一个通过new Vue创建...
    多多酱_DuoDuo_阅读 4,626评论 0 2
  • 每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: 实例生命周期钩子 每个 Vue 实例...
    Timmy小石匠阅读 5,186评论 0 11
  • 3. JSX JSX是对JavaScript语言的一个扩展语法, 用于生产React“元素”,建议在描述UI的时候...
    pixels阅读 7,913评论 0 24
  • 40、React 什么是React?React 是一个用于构建用户界面的框架(采用的是MVC模式):集中处理VIE...
    萌妹撒阅读 4,693评论 0 1