vue3 vite axios proxy config
1、vite.config.jsvite.config.js
server: { port: 5500, proxy: { '/testApi': { target: 'https://xxxx.xxxx.com/gateway', changeOrigin: true, rewrite: (path) => path.replace(/^\/testApi/, '') }, } }
2、.env.development
NODE_ENV = 'development'
VITE_HOST_URL = '/testApi'
3、request.js
// 创建axios实例 const service = axios.create({ baseURL: import.meta.env.VITE_HOST_URL, timeout: 60000, // 请求超时时间 })
4、.env.production
NODE_ENV = 'production'
VITE_HOST_URL = 'https://xxxx.xxxx.com/gateway