vue报错:Loading chunk * failed,vue-router懒加载出错问题。

目录

    • [一、 出现场景](#一、 出现场景)
    • [二、 解决方案](#二、 解决方案)

一、 出现场景

菜单路由跳转时,有时页面会卡死,无法进行路由跳转

二、 解决方案

修改src目录下permission.js文件

js 复制代码
router.onError((error) => {
  const jsPattern = /Loading chunk (\S)+ failed/g
  const cssPattern = /Loading CSS chunk (\S)+ failed/g
  const isChunkLoadFailed = error.message.match(jsPattern || cssPattern)
  const targetPath = router.history.pending.fullPath
  if (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')
    }
  }
})
相关推荐
小满zs1 小时前
Zustand 第五章(订阅)
前端·react.js
涵信2 小时前
第一节 基础核心概念-TypeScript与JavaScript的核心区别
前端·javascript·typescript
谢尔登2 小时前
【React】常用的状态管理库比对
前端·spring·react.js
编程乐学(Arfan开发工程师)2 小时前
56、原生组件注入-原生注解与Spring方式注入
java·前端·后端·spring·tensorflow·bug·lua
小公主2 小时前
JavaScript 柯里化完全指南:闭包 + 手写 curry,一步步拆解原理
前端·javascript
姑苏洛言4 小时前
如何解决答题小程序大小超过2M的问题
前端
TGB-Earnest5 小时前
【leetcode-合并两个有序链表】
javascript·leetcode·链表
GISer_Jing5 小时前
JWT授权token前端存储策略
前端·javascript·面试
开开心心就好5 小时前
电脑扩展屏幕工具
java·开发语言·前端·电脑·php·excel·batch
拉不动的猪5 小时前
es6常见数组、对象中的整合与拆解
前端·javascript·面试