From e60684b7c238e23d9fe166285bafd9370d6c9dae Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Wed, 28 Feb 2024 19:23:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D:=20=F0=9F=A7=A9=20?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/nginx.conf | 5 +++++ src/api/{ => v1}/test.ts | 0 src/router/index.ts | 4 ++-- src/utils/request-mock.ts | 23 +++++++++++++++++++++++ vue.config.js | 4 ++++ 5 files changed, 34 insertions(+), 2 deletions(-) rename src/api/{ => v1}/test.ts (100%) create mode 100644 src/utils/request-mock.ts diff --git a/docker/nginx.conf b/docker/nginx.conf index 5374e1c..9c2213a 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -14,6 +14,11 @@ server { proxy_pass http://192.168.3.98:1001; } + # mock 跨域 + location ~/api/ { + proxy_pass http://192.168.3.98:1001; + } + error_page 404 404.html; location = /50x.html { diff --git a/src/api/test.ts b/src/api/v1/test.ts similarity index 100% rename from src/api/test.ts rename to src/api/v1/test.ts diff --git a/src/router/index.ts b/src/router/index.ts index 44674b8..d72a640 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,4 +1,4 @@ -import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'; +import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router'; const routes: Array = [ { @@ -14,7 +14,7 @@ const routes: Array = [ */ const router = createRouter({ history: - process.env.NODE_ENV === 'production' ? createWebHashHistory(process.env.BASE_URL) : createWebHashHistory(process.env.BASE_URL), + process.env.NODE_ENV === 'production' ? createWebHashHistory(process.env.BASE_URL) : createWebHistory(process.env.BASE_URL), routes, }); diff --git a/src/utils/request-mock.ts b/src/utils/request-mock.ts new file mode 100644 index 0000000..1484775 --- /dev/null +++ b/src/utils/request-mock.ts @@ -0,0 +1,23 @@ +import axios from 'axios'; + +const request = axios.create({ + baseURL: '/mock', + timeout: 10000, +}); + +// 请求拦截器 +request.interceptors.request.use(config => { + return config; +}); + +// 响应拦截器 +request.interceptors.response.use( + response => { + return response.data; + }, + error => { + return Promise.reject(new Error('网络错误')); + }, +); + +export default request; diff --git a/vue.config.js b/vue.config.js index f20a569..7271944 100644 --- a/vue.config.js +++ b/vue.config.js @@ -27,6 +27,10 @@ module.exports = defineConfig({ target: process.env.VUE_APP_URL, changeOrigin: true, }, + '/mock': { + target: process.env.VUE_APP_URL, + changeOrigin: true, + }, }, }, configureWebpack: {