項目根目錄下創建 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