我正试图更改Axios补丁的基本URL,但我不知道为什么要调用它stageReceiver方法,它仍然得到一个默认的baseURL(我的npm运行服务正在监听和服务我的应用程序(。
import axios from "axios";
const getSingleSenders = baseURL =>
axios
.get("/single/senders", {baseURL: baseURL})
.then(({ data }) => ({ senders: data }))
const stageReceiver = (receiverId, baseURL, stagedPayload) =>
axios
.patch("/single/receivers/" + receiverId + "/staged", {
baseURL: baseURL,
payload: stagedPayload,
Headers: {"Content-Type": "application/json"}
})
.then(({ data }) => ({ receiver: data }))
export default {
getSingleSenders,
getSingleSender,
stageReceiver
};
当我在需要时调用函数时,让我们说newBaseURL=";http://172.17.0.13:8080/test">
const stagedResult = await connectionService.stageReceiver(id, newBaseURL, connectionPayload)
如果我在浏览器上查看,我可以看到请求得到404,其中包含我的Vue dev实例的错误URL(我的npm运行服务正在侦听(:
http://localhost:8082/single/receivers/id/staged
取而代之的是
http://172.17.0.13:8080/test/single/receivers/id/staged
带有get的另一个方法getSingleSenders可以正常工作,并且它可以正确编写baseURL。我错过了一些东西,但我不知道是什么。我希望这个问题足够清楚。非常感谢你的建议。
您应该将您的配置作为第三个参数传递,如文档中所述:
const stageReceiver = (receiverId, baseURL, stagedPayload) =>
axios
.patch(
"/single/receivers/" + receiverId + "/staged",
stagedPayload,
{
baseURL: baseURL,
Headers: { "Content-Type": "application/json" }
}
)
.then(({ data }) => ({ receiver: data }));