【发布时间】:2018-02-03 12:21:46
【问题描述】:
如何在axios 中创建动态主机?
示例:
const host = location.hostname;
// axios.defaults.baseURL = 'http://hastore.local';
axios.defaults.baseURL = host;
axios.defaults.port = 8080;
axios.get('api/categories')
.then((res) => {
this.categories = res.data;
console.log(res);
})
.catch((err) => {
console.warn('error during http call', err);
});
字符串axios.defaults.baseURL = 'http://hastore.local'; 不适合,因为在生产中不起作用。
String const host = location.hostname; 也不是解决方案,因为我得到了不正确的端口并复制了主机。
目标是根据环境获得合适的主机。 我阅读了很多关于此的文章,但我没有找到解决方案。 感谢您的帮助!
- axios 版本:例如:v0.16.2
- 环境:例如:node v8.9.4、chrome 64.0.3282.119、Ubuntu 16.04
- Symfony 4.0.4
- Vue.js 2.4.2
- vue-axios 2.0.2
【问题讨论】:
标签: symfony vue.js axios symfony4