Annie 4 semanas atrás
pai
commit
2817d70448
3 arquivos alterados com 26 adições e 6 exclusões
  1. 1 1
      .env.development
  2. 1 1
      src/router/index.js
  3. 24 4
      vue.config.js

+ 1 - 1
.env.development

@@ -5,7 +5,7 @@ VUE_APP_TITLE = 智能产线管理系统
 ENV = 'development'
 
 # 智能产线管理系统/开发环境
-VUE_APP_BASE_API = 'dev-api'
+VUE_APP_BASE_API = '/dev-api'
 
 # 路由懒加载
 VUE_CLI_BABEL_TRANSPILE_MODULES = true

+ 1 - 1
src/router/index.js

@@ -178,7 +178,7 @@ Router.prototype.replace = function push(location) {
 
 export default new Router({
   mode: 'history', // 去掉url中的#
-  base: '/admin',
+  base: process.env.NODE_ENV === 'production' ? '/admin' : '/',
   scrollBehavior: () => ({ y: 0 }),
   routes: constantRoutes
 })

+ 24 - 4
vue.config.js

@@ -34,13 +34,33 @@ module.exports = {
     port: port,
     open: true,
     proxy: {
-      // detail: https://cli.vuejs.org/config/#devserver-proxy
+      // 参考:https://cli.vuejs.org/config/#devserver-proxy
       [process.env.VUE_APP_BASE_API]: {
         target: `http://localhost:8087`,
         changeOrigin: true,
-        pathRewrite: {
-          ['^' + process.env.VUE_APP_BASE_API]: ''
-        }
+        // 修正 pathRewrite,确保原始路径正确返回
+        // pathRewrite: (pathReq, req) => {
+        //   const baseApi = process.env.VUE_APP_BASE_API;
+        //   // 兼容 baseApi 为空或未定义的情况
+        //   if (!baseApi) {
+        //     console.error('[Proxy PathRewrite] VUE_APP_BASE_API 未配置,无法进行路径重写');
+        //     return pathReq;
+        //   }
+        //   // 只替换以 baseApi 开头的路径,避免误替换
+        //   if (pathReq.startsWith(baseApi)) {
+        //     const newPath = pathReq.replace(new RegExp('^' + baseApi), '');
+        //     console.log(`[Proxy PathRewrite] 原始路径: ${pathReq},重写后: ${newPath},${newPath === pathReq ? '未发生路径重写,请检查 VUE_APP_BASE_API 配置' : '路径重写成功'}`);
+        //     return newPath;
+        //   } else {
+        //     // 如果路径未以 baseApi 开头,直接返回原始路径并提示
+        //     console.warn(`[Proxy PathRewrite] 原始路径: ${pathReq} 未以 ${baseApi} 开头,未进行重写`);
+        //     return pathReq;
+        //   }
+        // },
+        // logLevel: 'debug',
+        // onProxyReq: (proxyReq, req) => {
+        //   console.log('[HPM]', process.env.NODE_ENV, req.method, req.originalUrl, '->', req.url);
+        // }
       }
     },
     disableHostCheck: true