本文相关视频教程:https://www.bilibili.com/video/BV1Fi4y1q74p?p=46&vd_source=2894aa0e46c09ba98269f266128b6c6e
有些特殊情况需要部署到子路径下,例如:https://www.ruoyi.vip/admin,以下是若依ruoyi-vue部署在域名的子目录下的配置步骤:
- 修改
vue.config.js
中的publicPath
:
// 部署生产环境和开发环境下的URL。
// 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
// 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
publicPath: process.env.NODE_ENV === "production" ? "/admin" : "/admin",
- 修改router/index.js:
添加一行base属性
export default new Router({
base: "/admin",
mode: 'history', // 去掉url中的#
scrollBehavior: () => ({ y: 0 }),
routes: constantRoutes
})
- /index路由添加获取子路径/admin:
修改layout/components/Navbar.vue中的location.href
async logout() {
this.$confirm('确定注销并退出系统吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.$store.dispatch('LogOut').then(() => {
location.href = '/admin/index';
})
}).catch(() => {});
}
修改utils/request.js中的location.href
location.href = '/admin/index';
- 修改nginx配置:
location /admin {
alias /home/ruoyi/projects/ruoyi-ui;
try_files $uri $uri/ /admin/index.html;
index index.html index.htm;
}