之前项目一直配置的hash模式,今天折腾了一下,搞定了history同二级路由部署流程,以下拿/admin做二级路径举例
vue项目需要配置的点
- vue.config.js publicPath,这里可以用env配置文件
#.env.development
VUE_APP_BASE_URL = '/'
#.env.production
VUE_APP_BASE_URL = '/admin/'
#vue.config.js
module.exports = {
publicPath: process.env.VUE_APP_BASE_URL,
//略
}
- vue router
export default new Router({
mode: 'history',
base: process.env.VUE_APP_BASE_URL, //项目部署再二级路径,同时是
scrollBehavior: () => ({y: 0}),
routes: constantRoutes
})
- public/index.html 如果项目有在该文件配置了一些资源链接,也需要加上二级路径前缀
<link rel="icon" href="<%= BASE_URL %>favicon.ico"> //BASE_URL是publicPath这里配置的
nginx配置文件需要修改的点
官方文档给的例子是如下
https://router.vuejs.org/zh/guide/essentials/history-mode.html#nginx
location / {
try_files $uri $uri/ /index.html;
}
但是我们项目一般在二级路径的就要改一下:
location ^~ /admin/ {
alias /ruoyi-ui/dist/;
index index.html index.htm;
try_files $uri $uri/ /admin/index.html;
}
注意配置文件不要有错,比如缺少分号啥的,之前就是缺个分号,重启半天不生效浪费时间了。
Q.E.D.