关于“ResizeObserver loop limit exceeded”引申的代码/工具/环境问题

363 天前
 OldCarMan

rt,最近学前端(vue3+andv),相信不少人遇到过此类问题,这个问题是在浏览器控制台和 webstorm npm 服务器输出日志都没报错的情况下发生的。我是第一次遇到此类问题。

ResizeObserver loop limit exceeded
    at eval (webpack-internal:///./node_modules/webpack-dev-server/client/overlay.js:296:58)
<template>
  <HelloWorld />
</template>

<script setup>
import HelloWorld from './components/HelloWorld.vue'
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
<template>
  <a-tabs v-model:activeKey="activeKey" class='test-tab'>
    <a-tab-pane key="1" tab="Tab 1">Tab 1</a-tab-pane>
    <a-tab-pane key="2" tab="Tab 2" >Tab 2</a-tab-pane>
    <a-tab-pane key="3" tab="Tab 3">Tab 3</a-tab-pane>
  </a-tabs>
  <button @click="onTell" style='width: 50px;height: 50px;background: #42b983'>click</button>
</template>
<script setup>
import {ref} from "vue";

const activeKey = ref('1');
function onTell() {
  doSomething();
}

function doSomething() {
  removeLink();
  createLink('/test.css');
}

function removeLink() {
  hide();
  document.querySelectorAll("link[test=aa]").forEach(item => {
    item.remove()
  });
}

function createLink(path) {
  const link = document.createElement('link');
  link.setAttribute('rel', 'stylesheet');
  link.setAttribute('type', 'text/css');
  link.setAttribute('test', 'aa');
  link.setAttribute('href', path);
  // link.onload = () => {
  //   show();
  // };
  document.head.appendChild(link)
  show();
}

function hide() {
  document.querySelectorAll(".test-tab").forEach(item => {
    if (item.style.display === 'block' || item.style.display === '') {
      item.style.display = 'none'
    }
  });
}

function show() {
  document.querySelectorAll(".test-tab").forEach(item => {
    if (item.style.display === 'none') {
      item.style.display = 'block'
    }
  });
}
</script>
import { createApp } from "vue";
import App from "./App.vue";
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';

const app = createApp(App).use(Antd);
app.mount("#app");
    "ant-design-vue": "^3.2.20",
    "core-js": "^3.8.3",
    "vue": "^3.2.13"
网上很多人都有类似的问题,但类似问题的背后,根本的原因又各种各样,琳琅满目。所以想请教一下各位前端大佬,这类问题要怎么排查好?除了减法操作,逐个排查外,有什么办法能够比较快速的定位到问题所在。另外这大概是什么问题?工具问题( webpack )?环境问题?还是组件渲染尺度匹配问题...?

谢谢大家回复,祝大家节日愉快!

1067 次点击
所在节点    前端开发
2 条回复
clue
346 天前
其实社区已经有很多答案了,我帮你总结下
https://stackoverflow.com/questions/49384120/resizeobserver-loop-limit-exceeded

1. 这个错误本质是在 ResizeObserver 的回调中同步修改影响了触发元素的大小,会重新触发 resize ,相当于一个死循环;想要解决,就避免修改影响触发元素,或者你确保不会无限循环时把修改动作放到下个任务中处理(比如 requestAnimationFrame )
2. 这个错本身是可以无视的,如果你不监听 window.onerror ,甚至在 console 中都不会出现,只有你用 webpack-dev-server 并开启了 client.overlay 时才影响,可以考虑把它关掉
OldCarMan
344 天前
@clue 学习了,谢谢告知。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/936833

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX