列表渲染-infinite update loop in a component render function

模式苔原
• 阅读 1568

列表渲染-infinite update loop in a component render function
错误:组件渲染函数中可能有无限的更新循环。
原因:渲染组件的时候,使用计算属性或者方法去改变了data里面的数据,data里面的数据变化又会调用render函数,从新渲染组件,这样就造成了死循环。

例子:

<template>
  <div id="app">
    <table>
      <thead>
      </thead>
      <tbody>
      <tr v-for="item in arr1" :key="item">
          <td >{{getIndex()}}</td>
      </tr>
      <tr v-for="item in arr2" :key="item">
          <td>{{getIndex()}}</td>
      </tr>

      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: 'App',
  data () {
    return {
      arr1: [1, 2],
      arr2: [3, 4, 5],
      num: 0
    }
  },
  methods: {
    getIndex: function () {
      return ++this.num
    }
  }
}
</script>

既然列表渲染时调用getIndex更改num会重新触发渲染,那么我们可以将data里面num变量提到外面作为局部变量

<template>
  <div id="app">
    <table>
      <thead>
      </thead>
      <tbody>
      <tr v-for="item in arr1" :key="item">
          <td >{{getIndex()}}</td>
      </tr>
      <tr v-for="item in arr2" :key="item">
          <td>{{getIndex()}}</td>
      </tr>

      </tbody>
    </table>
  </div>
</template>

<script>
var num = 0
export default {
  name: 'App',
  data () {
    return {
      arr1: [1, 2],
      arr2: [3, 4, 5]
    }
  },
  methods: {
    getIndex: function () {
      return ++num
    }
  }
}
</script>

参考:
https://blog.csdn.net/a5534789/article/details/82780298

https://segmentfault.com/a/1190000011156865?utm_source=tag-newest

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
梦
5年前
微信小程序new Date()转换时间异常问题
微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT
Wesley13 Wesley13
4年前
thinkphp3.2.3模板渲染支持三元表达式
thinkphp3.2.3模板渲染支持三元表达式{$status?'正常':'错误'}{$info'status'?$info'msg':$info'error'}注意:三元运算符中暂时不支持点语法。如下:           <divclass"modalhidefade"id'myModa
Wesley13 Wesley13
4年前
VBox 启动虚拟机失败
在Vbox(5.0.8版本)启动Ubuntu的虚拟机时,遇到错误信息:NtCreateFile(\\Device\\VBoxDrvStub)failed:0xc000000034STATUS\_OBJECT\_NAME\_NOT\_FOUND(0retries) (rc101)Makesurethekern
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
4年前
oracle游标的例子
declare    cursor ca is select id_no, name from user where ym201401;begin    for cb in ca loop        update path set enamecb.name where id_nocb.id
Stella981 Stella981
4年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
模式苔原
模式苔原
Lv1
放下屠刀,立地成佛、救人一命,胜造七级浮屠。
文章
3
粉丝
0
获赞
0