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

在改一个项目的过程中,与到一个问题,route.push()的时候报错:Loading chunk * failed。在网上找了一大堆解决办法,虽然没有直接的解决问题,但还是得到了一些启发。

作者这里提供了三种解决方式:

一、不用懒加载,直接使用组件

import Foo from "@/views/foo"

const router = new VueRouter({
  routes: [
    { path: '/foo', component: Foo }
  ]
})

二、继续使用懒加载,但是在需要在路由发生错误时做一些处理。

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://blog.csdn.net/zhongguohaoshaonian/article/details/95179057

三、检查是否是vue.config.js配置问题

由于之前打包生产环境时为解决静态资源访问的问题,在vue.config.js中配置了publicPath变量,改回如下即可:

module.exports = {
    publicPath: "/"
}

具体情况需要根据实际遇到的问题来解决,这里只是提供一些思路。

发布了77 篇原创文章 · 获赞 16 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_41756580/article/details/105246352