vue项目Loading chunk {n} failed

2021-09-26  本文已影响0人  _半城

在运行一个vue项目时,切换路由页面白屏,报错如下:

Error:Loading chunk 0 failed.

意思是某些js bundle没找到

解决方法:通过router.onError捕获错误,当捕获到Loading chunk {n} failed的错误时我们重新渲染目标页面

router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
});

参考文章:
https://segmentfault.com/a/1190000016382323

上一篇 下一篇

猜你喜欢

热点阅读