关于vite:The-server-responded-with-a-nonJavaScript-MIME-type-oftexthtml

21次阅读

共计 826 个字符,预计需要花费 3 分钟才能阅读完成。

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…..

的确应该是 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'));
    }
]

后续继续跟进中 …

正文完
 0