axios中的http中的baseURL怎么用?

本文介绍了在axios中如何设置baseURL,并展示了如何在代理配置proxyTable中处理baseURL,使得多个接口可以统一处理,简化配置。通过示例代码,展示了如何将baseURL设置为'/service',并在代理时将'/service'路径重写,以实现对'http://222.85.148.140:5000'的目标地址的请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先给最外层包一个service,也就是每个接口外层都有一个/service了
export const http = axios.create({
  baseURL: '/service',
  withCredentials: false,
  //headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
});
export const http1 = axios.create({
  baseURL: '/service',
  withCredentials: false
});
export const httpLayer = axios.create({
  baseURL: '/service',
  withCredentials: false
});
然后代理的时候遇到下面的情况:
之前是/publicService'单独写一次代理,现在由于都包了一层
/service,所以就代理一次就好了 
proxyTable: { '/service': { target: 'http://222.85.148.140:5000', changeOrigin: true, pathRewrite: function (path, req) { return path.replace('/service', ''); } }, '/publicService': { target: 'http://222.85.148.140:5000', changeOrigin: true, pathRewrite: function (path, req) { return path.replace('/service', ''); } },},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值