项目根目录下创建 vue.config.js
module.exports = {
devServer: {
proxy: {
'/api': {//代理 /api/xxx/xxx 的请求
target: '你要请求接口的地址',
pathRewrite: {
'^/api': ''
}
}
},
}
}
开发环境的 BASE_URL 如下
const BASE_URL = "/api";
request 的 url 为 /xxx/yyy
会被代理到 target/xxx/yyy
参考:
https://www.webpackjs.com/configuration/dev-server/#devserver