前言

  • RuoYi-Vue 3.8.4

1. 在ENV中,添加 VUE_APP_BASE_URL 配置项

  1. .env.production 文件中添加 VUE_APP_BASE_URL 配置项 VUE_APP_BASE_URL = '/xxx'
  2. .env.development 文件中也添加。
  3. .env.staging 文件中也添加。

2. 在 vue.config.js 文件中,修改 publicPath 配置项

  //publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  publicPath: process.env.VUE_APP_BASE_URL,

3. 在 src/utils/request.js 文件中,修改 location.href 的地址

//location.href = '/index';
location.href = process.env.VUE_APP_BASE_URL + '/index';

4. 在 src/router/index.js文件中,添加 base 配置项

export default new Router({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

修改为:

export default new Router({
  base: process.env.VUE_APP_BASE_URL,
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

5. 在 src/layout/components/Navbar.vue 文件中,修改 location.href 的地址

//location.href = "/index";
location.href = process.env.VUE_APP_BASE_URL + '/index';

6. nginx 配置中,新增一条try_files

location /xxx/ {
	#root   /home/ruoyi/projects/ruoyi-ui;
	alias  /home/ruoyi/projects/ruoyi-ui/xxx/;
	try_files $uri $uri/ /xxx/index.html;
	index  index.html index.htm;
}

参考

RuoYi-Vue 前端分离版:nginx配置选项 try_files
vue项目配置env

Logo

快速构建 Web 应用程序

更多推荐