在运行一个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);
}
});