1、问题:
在写Vite+vue3.0项目时,配置vite代理,遇到不起效的问题,具体如下:
// vite.config.ts proxy: { '/api': ' http://localhost:3000' }
如上简写的写法,代理不生效。
2、解决方案:
查看官网后,改用如下写法生效:
//vite.config.ts proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, '') } }
3、原因排除:
经过测试发现,不添加“rewrite”属性时,代理配置仍存在不生效的情况,
例如:
// 此写法可能存在代理不生效的情况 proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true } }