diff --git a/vite.config.js b/vite.config.js index ab594bd..4c589f4 100644 --- a/vite.config.js +++ b/vite.config.js @@ -17,6 +17,7 @@ export default defineConfig(({ mode }) => { } } return { + base: './', // 设置相对路径 // 服务插件 plugins: [ json5Plugin(), diff --git a/vue/.env b/vue/.env index d8ece9f..bb0c15d 100644 --- a/vue/.env +++ b/vue/.env @@ -1 +1 @@ -VITE_BASE_URL="/api/v1" +VITE_BASE_URL="api/v1" diff --git a/vue/src/api/http.js b/vue/src/api/http.js index 1f48a1d..8282744 100644 --- a/vue/src/api/http.js +++ b/vue/src/api/http.js @@ -1,9 +1,16 @@ import axios from 'axios' +// 获取动态子路径并构建 base URL +const getBaseURL = () => { + let pathname = window.location.pathname; + pathname = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname; + return `${pathname}/${import.meta.env.VITE_BASE_URL}`; // 根据实际情况调整 +} + // axios对象实例 const http = axios.create({ // baseURL不设置,会自动使用当前域名 - baseURL: import.meta.env.VITE_BASE_URL, + baseURL: getBaseURL(), timeout: 60000 }) diff --git a/vue/src/router/index.js b/vue/src/router/index.js index 82ac4ac..178b0a0 100644 --- a/vue/src/router/index.js +++ b/vue/src/router/index.js @@ -1,4 +1,4 @@ -import { createRouter, createWebHistory } from 'vue-router' +import { createRouter, createWebHashHistory } from 'vue-router' const routes = [ { @@ -71,8 +71,7 @@ const routes = [ ] const router = createRouter({ - history: createWebHistory(), - base: '/', + history: createWebHashHistory(), routes })