Vue项目打包后空白问题通常与以下两个配置有关,建议按顺序检查:
1、修改vue.config.js配置(如没有则新建):
module.exports = {
publicPath: './', // 关键配置:使用相对路径
outputDir: 'dist',
assetsDir: 'static',
productionSourceMap: false
}
2、在路由配置文件(通常是router/index.js)中修改路由模式:
const router = new VueRouter({
mode: 'hash', // 改为hash模式,history模式需要服务器配置
base: process.env.BASE_URL,
routes
})
关键修改点说明:
- publicPath: './' 使打包后的静态资源使用相对路径
- 使用hash路由模式可避免页面刷新404问题
修改完成后重新打包测试:
npm run build
测试打包结果建议使用本地服务器:http-server的安装和运行
npm install -g http-server
cd dist
http-server
如果仍然空白,请检查以下内容:
- 控制台是否有资源加载错误
- 组件中使用的绝对路径资源(如你的iframe地址是否支持HTTPS)
- 是否配置了正确的404回退路由