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之后此问题已不存在,各种浏览器多次测试后也没有触发,如果还报类似的错误,请仔细检查异步路由导入写法是否正确。

后续持续跟进中...

posted @   天渺工作室  阅读(1331)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示