在運行一個vue項目時囊榜,切換路由頁面白屏迄靠,報錯如下:
Error:Loading chunk 0 failed.
意思是某些js bundle沒找到
解決方法:通過router.onError捕獲錯誤筒占,當(dāng)捕獲到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);
}
});