vite:
找到vite.config.ts
import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [uni()],
// 本地运行配置,及反向代理配置
server: {
cors: true, // 默认启用并允许任何源
open: true, // 在服务器启动时自动在浏览器中打开应用程序
//反向代理配置,注意rewrite写法,开始没看文档在这里踩了坑
proxy: {// 本地开发环境通过代理实现跨域,生产环境使用 nginx 转发
'/dev-api': {
target: 'http:******', // 通过代理接口访问实际地址。这里是实际访问的地址。vue会通过代理服务器来代理请求
changeOrigin: true,
ws: true, // 允许websocket代理
rewrite: (path) => path.replace(/^\/dev-api/, '') // 将api替换为空
}
}
}
});
webpack:
找到vue.config.js
devServer: {
host: '0.0.0.0',
port: 8082,
open: true,
proxy: {
// detail: https://cli.vuejs.org/config/#devserver-proxy
'': {
target: `http://****`,
changeOrigin: true,
pathRewrite: {
'^': ''
}
}
}
},