Failed to load module script: The server responded with a non-JavaScript MIME type of “text/html”.

vue3+vite打包以后,项目切换路由触发(偶发触发)报:

After using vue-router, there is an error in packaging and running # Failed to load module script: The server responded with a non-JavaScript MIME type of “text/html”. Strict MIME type checking is enforced for module scripts per HTML spec.

Failed to fetch dynamically imported module…

Alt

确实应该是vite2.xx的bug https://github.com/vitejs/vite/issues/863
注意: 在使用vue3+vite时候,只发现vue-router切换时触发此现象

问题:

// vue3+vite此种异步路由引入方法 开发调试时确实没有问题,但是vite打包以后就会触发报错
routes: [
    {
      path: "/",
      name: "Home",
      component: import(`../views/${view}.vue`)
    }
]

解决方法:

// 这样引用虽然解决了问题(浏览器会提醒,不用管),但是在一些win10自带的低版本edge浏览器中还是会稀有触发
// 应该确实是vite的兼容性bug问题
import { defineAsyncComponent } from 'vue'; //异步组挂载器
routes: [
    {
      path: "/",
      name: "Home",
      component: defineAsyncComponent(() => import('../views/Home.vue'));
    },
    {
      path: "/about",
      name: "About",
      component: defineAsyncComponent(() => import('../views/About.vue'));
    }
]

————————————————————
2023年年初更新
Vite在升级到Vite3.x之后此问题已不存在,各种浏览器多次测试后也没有触发,如果还报类似的错误,请仔细检查路由异步导入写法是否正确。

后续持续跟进中…

  • 14
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值