gpt4 book ai didi

javascript - 从 vue-resource 切换到 axios

转载 作者:数据小太阳 更新时间:2023-10-29 04:39:52 26 4
gpt4 key购买 nike

使用 vue-resource,我们可以像这样在 main.js 中设置根 url:

Vue.http.options.root = 'http://localhost:3000/api'

我尝试将其替换为:

axios.defaults.baseURL = 'http://localhost:3000/api';
Vue.prototype.$http = axios

但是,现在我的 post 调用没有按预期工作,并且 Vue.http.post 抛出错误。

这是如何实现的?

最佳答案

使用 axios,可以创建另一个实例 having a custom config

var my_axios = axios.create({
baseURL: 'http://localhost:3000/api',
});

从这里可以使用my_axios 进行操作。您可以将自定义 axios 实例原型(prototype)化到 Vue 中:

Vue.prototype.$http = my_axios

关于javascript - 从 vue-resource 切换到 axios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41879928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com