这个代码为什么会出现[Vue warn]: Maximum recursive updates exceeded in component <Grammar>. This means you have

这个代码为什么会出现[Vue warn]: Maximum recursive updates exceeded in component <Grammar>. This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself. Possible sources include component template, render function, updated hook or watcher source function这个错误。

<template v-for="(item, index) in flattenSpans" :key="index">
    <span class="sentence" :style="getBackgroundColor(item.w_span.label)">{{ item.text }}        
    </span>
 </template>


function getBackgroundColor(label) {
  let color
  switch (label) {
    case '陈述句/祈使句':
      color = '#F9FBE8'
      break
    case '从句':
      color = '#FFF3E1'
      break
    case '特殊疑问句':
      color = '#FCE5ED'
      break
    case '倒装句':
      color = '#F3E6F5'
      break
    case '一般疑问句':
      color = '#FFECEF'
      break
    case '介词短语':
      color = '#FFF8E2'
      break
    case '非谓语':
      color = '#E2F5FE'
      break
  }
  if (!oldLabel.value || oldLabel.value == label) {
    oldLabel.value = label
    return { backgroundColor: color }
  } else if (oldLabel.value != label) {
    oldLabel.value = label
    return {
      backgroundColor: color,
      marginLeft: '4.8px'
    }
  }
}

原因: 这个错误可能是由于你的 getBackgroundColor 函数在逻辑中修改了响应式数据并触发了无限递归。在Vue.js中,一些触发更新的操作,如果不谨慎使用,可能导致无限递归更新。

解决:把let oldLabel = ref()改为let oldLabel

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值