vue报错:Loading chunk * failed,vue-router懒加载出错问题。
目录
- 一、 出现场景
- 二、 解决方案
一、 出现场景
菜单路由跳转时,有时页面会卡死,无法进行路由跳转
二、 解决方案
修改src目录下permission.js文件
router.onError((error) => {const jsPattern = /Loading chunk (\S)+ failed/gconst cssPattern = /Loading CSS chunk (\S)+ failed/gconst isChunkLoadFailed = error.message.match(jsPattern || cssPattern)const targetPath = router.history.pending.fullPathif (isChunkLoadFailed) {localStorage.setItem('targetPath', targetPath)window.location.reload()}
})router.onReady(() => {const targetPath = localStorage.getItem('targetPath')const tryReload = localStorage.getItem('tryReload')if (targetPath) {localStorage.removeItem('targetPath')if (!tryReload) {router.replace(targetPath)localStorage.setItem('tryReload', true)} else {localStorage.removeItem('tryReload')}}
})