瀏覽代碼

build: use server proxy instead of baseURL in request.js

icenaked 3 年之前
父節點
當前提交
8ec7581c1f
共有 2 個文件被更改,包括 19 次插入9 次删除
  1. 3 2
      src/utils/request.js
  2. 16 7
      vite.config.js

+ 3 - 2
src/utils/request.js

@@ -4,8 +4,9 @@ const service = axios.create({
     // process.env.NODE_ENV === 'development' 来判断是否开发环境
     // process.env.NODE_ENV === 'development' 来判断是否开发环境
     // easy-mock服务挂了,暂时不使用了
     // easy-mock服务挂了,暂时不使用了
     // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
     // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
-    baseURL: 'http://localhost:8080',
-    //timeout: 5000
+    //baseURL: 'http://localhost:8080',
+    baseURL: "",
+    timeout: 5000
 });
 });
 
 
 service.interceptors.request.use(
 service.interceptors.request.use(

+ 16 - 7
vite.config.js

@@ -7,14 +7,23 @@ export default {
         include: ['schart.js']
         include: ['schart.js']
     },
     },
 
 
-    server:{
+    // server:{
+    //     proxy: {
+    //         '/api': 'http://114.212.245.120:7788/',
+    //         // '/api': {
+    //         //     target: 'http://114.212.245.120:7788/',
+    //         //     changeOrigin: true,
+    //         //     // rewrite: path => path.replace(/^\/api/, '')
+    //         // }
+    //     }
+    // },
+    server: {
         proxy: {
         proxy: {
-            '/api': 'http://114.212.245.120:7788/',
-            // '/api': {
-            //     target: 'http://114.212.245.120:7788/',
-            //     changeOrigin: true,
-            //     // rewrite: path => path.replace(/^\/api/, '')
-            // }
+            '/api': {
+                target: 'http://localhost:8080/api',
+                changeOrigin: true,
+                rewrite: (path) => path.replace(/^\/api/, '') // 不可以省略rewrite
+            }
         }
         }
     },
     },
     devServer: {
     devServer: {