【发布时间】:2018-05-04 14:09:44
【问题描述】:
我已经像这样配置了我的 axios
const axiosConfig = {
baseURL: 'http://127.0.0.1:8000/api',
timeout: 30000,
};
Vue.prototype.$axios = axios.create(axiosConfig)
在我的组件中,我拨打电话
this.$axios.get('items').then()..
现在上述方法有效,但我想在不影响全局基本 URL 的情况下更改 baseURL,以便在我的组件中我可以在没有 API 端点的情况下简单地使用它
我试过了
this.$axios.baseURL = "http://127.0.0.1:8000";
this.$axios.get().. //this is still in api endpoint
我该怎么做?
【问题讨论】:
-
你可以通过设置
this.$axios.defaults.baseURL = "https://localhost:8000"来改变axios实例的base url -
对于其他访问者,要临时更改baseURL,您可以将绝对url传递给所需的方法:
this.$axios.get('http://127.0.0.1:8000/items') -
axios.get('/items', { baseUrl: 'https://another-endpoint' }或另一个示例axios.post('/items', payload, { baseUrl: 'https://another-endpoint' })是另一种选择。请参阅github.com/axios/axios#config-order-of-precedence 了解配置优先顺序。 -
那么用baseURL创建实例有什么用呢?
axios.create(axiosConfig)
标签: javascript vue.js vuejs2 axios