在vue.config.js里配置:
module.exports = {
devServer: {
open: true,
// 跨域代理
proxy: {
'/api': {
target: "http://xxx.xxx.xxx(后端地址)", // 后台接口域名
pathRewrite: {
'^/api': '' //路径重写
},
changeOrigin: true //是否跨域
}
}
}
}
在axios中使用:
const instance = axios.create({
baseURL: '/api',
timeout: 15000,
})