更改 axios 的默认基本 url [英] Change the default base url for axios
本文介绍了更改 axios 的默认基本 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经这样配置了我的 axios
I have configured my axios like this
const axiosConfig = {
baseURL: 'http://127.0.0.1:8000/api',
timeout: 30000,
};
Vue.prototype.$axios = axios.create(axiosConfig)
在我的组件内部,我调用了
Inside my component i make a call as
this.$axios.get('items').then()..
现在上述工作,但我想改变 baseURL
而不影响全局基本 URL,这样在我的组件中我可以简单地使用没有 API 端点所以
Now the above works but i would like to change the baseURL
without affecting the global base URL so that in my component i can simply use without API endpoint so
我试过了
this.$axios.baseURL = "http://127.0.0.1:8000";
this.$axios.get().. //this is still in api endpoint
我该怎么做?
推荐答案
代替
this.$axios.get('items')
使用
this.$axios({ url: 'items', baseURL: 'http://new-url.com' })
如果你不通过method: 'XXX'
那么默认情况下,它会通过get
方法发送.
If you don't pass method: 'XXX'
then by default, it will send via get
method.
请求配置: https://github.com/axios/axios#request-config
这篇关于更改 axios 的默认基本 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文