项目hbuilder运行报错加载报错Failed to load module script: Expected a JavaScript-or-Wasm module script but the
加载报错Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of “”. Strict MIME type checking is enforced for module scripts per HTML spec.,
这个 MIME type 错误是 HBuilder 真机运行的典型问题。
vite.config.js
将base设置为./
base: "./", // HBuilder 真机必须用相对路径 builder:{ assetsInlineLimit: 4096, // 小于 4kb 的资源内联为 base64 rollupOptions: { output: { // 手动分包,避免单文件过大 manualChunks: { 'vue-vendor': ['vue', 'vue-router', 'pinia'], 'vuetify': ['vuetify'], 'echarts': ['echarts'] }, // 确保资源路径正确 chunkFileNames: 'assets/js/[name]-[hash].js', entryFileNames: 'assets/js/[name]-[hash].js', assetFileNames: 'assets/[ext]/[name]-[hash].[ext]' } } } router/index.js
使用hash模式,兼容真机运行
import { createRouter, createWebHashHistory } from "vue-router"; const router = createRouter({ // 使用 hash 模式,兼容 HBuilder 真机运行 history: createWebHashHistory(), routes, /**scrollBehavior:路由切换时控制页面滚动位置: * 如果有 savedPosition(浏览器前进/后退时),保持原位置;否则默认滚动到页面顶部。 */ scrollBehavior(to, from, savedPosition) { if (savedPosition) { return savedPosition; } else { return { top: 0 }; } }, });