vite.config.js
proxy: {
// 把key的路径代理到target位置
// detail: https://cli.vuejs.org/config/#devserver-proxy
[process.env.VITE_BASE_API]: { // 需要代理的路径 例如 '/api'
target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`,
// 代理到 目标路径
changeOrigin: true,
rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),
},
[process.env.VITE_MOCK_BASE_API]: {
target: `${process.env.VITE_MOCK_BASE_PATH}`,
changeOrigin: true,
//假如在请求拦截器上写了baseURL 就rewrite 否则就不添加
}
},
.env
VITE_SERVER_PORT = 8888
VITE_BASE_API = /api //项目baseURL
VITE_BASE_PATH = xxx //项目接口真实地址
VITE_MOCK_BASE_PATH = xxx // fastmock地址
VITE_MOCK_BASE_API = /mockApi //需要mock baseURL
axios
service.interceptors.request.use(
config => {
config.baseURL = config.url.includes('mockApi') ? '/' : import.meta.env.VITE_BASE_API
return config
},