[英]How to make dynamic baseURL in an Axios instance
My application is running in iframe
and it gets a payload with baseURL
during loading.我的应用程序在
iframe
运行,它在加载过程中使用baseURL
获取有效负载。 I want to use that url to create Axios instance.我想使用该 url 来创建 Axios 实例。
Is there a way to do that?有没有办法做到这一点?
I mean:我的意思是:
const instance = axios.create({
baseURL: 'https://some-domain.com/api/', // should be dynamic
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
Try this way.试试这个方法。
//config.js
var config = {
localConfig: {
baseurl: 'http://localhost:8080/'
},
devConfig: {
//dev url
baseurl: 'http://dev.url:8080/'
},
prodConfig: {
//prod url
baseurl: 'http://production.url:8080/'
}
},
getConfig: function(){
switch(window.location.hostname) {
case 'localhost:8080/':
return this.localConfig;
case 'development':
return this.devConfig;
case 'production':
return this.prodConfig;
}
}
}
export default config;
//service.js
const instance = axios.create({
baseURL: config.getConfig().baseurl, // should be dynamic
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.