前言
默认情况下,使用 npm run build
打包后的 index.html
无法直接访问,需要nginx
转发或使用node
启用简单http serve
等方式实现。
实际开发中,有时需临时打开前端项目,若可以直接打开打包后的文件,对某些场景下他人简单使用或调试较为方便。
实现
- 确保
vue-router
模式为hash
模式 - 修改
vue.config.js
中publicPath
为./
即可 - 若
index.html
有文件的引用,修改为正确(相对)路径即可
router.js
const router = new VueRouter({
mode: "history",// 注释该行即可,默认使用 hash 模式
base: process.env.BASE_URL,
routes,
});
vue.config.js
module.exports = {
// 根路径 默认使用/ vue cli 3.3+ 弃用 baseUrl
publicPath: '/' // 此处改为 './' 即可
}
Tips:
vue cli
默认不会生成vue.config.js
,根目录下手动创建即可
vue下webpack
简单配置可参考文章 vue中webpack简单配置
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="./favicon.ico" />
<title><%= htmlWebpackPlugin.options.title %></title>
<script src="./script/SkeyeWebPlayer_2.0.1.min.js"></script>
</head>
<body>
......
</body>
</html>
tip: 具体html的修改根据实际情况而定,以上是我的项目修改地方:
link
中的href在这里插入代码片
属性地址需要从绝对路径
改为相对路径
"./favicon.ico
";
引入的SkeyeWebPlayer_2.0.1
插件,也需要改为相对地址“./script/SkeyeWebPlayer_2.0.1.min.js
”