Symfony axios中的动态主机
如何在Symfony axios中的动态主机,symfony,vue.js,axios,symfony4,Symfony,Vue.js,Axios,Symfony4,如何在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;
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='0http://hastore.local';代码>不适合,因为on-prodaction不工作
字符串const host=location.hostname代码>也不是解决方案,因为我得到了不正确的端口和双副本主机
目标是根据环境获得正确的主机。
我读了很多关于这方面的文章,但我没有找到解决办法。
谢谢你的帮助
- axios版本:例如:v0.16.2
- 环境:例如节点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
您可能不需要设置baseURL。您是否在每次发出请求时都尝试定义baseURL?比如说,
axios.get(`${host}:${port}/api/categories`)
我找到了一个解决方案。将此代码添加到:/src/main.js
例如:
const baseURL = 'http://localhost:8080';
if (typeof baseURL !== 'undefined') {
Vue.axios.defaults.baseURL = baseURL;
}
在这方面帮助我的答案是:
谢谢大家的帮助 是的,我需要在每次发出请求时尝试定义baseURL。使用环境变量是个好主意,但我不确定我是否有权访问它们@AchmedzianovDanilianI在控制台console.log(process.env.HOST)中打印时获得未定义的当然,在调用构建之前,您必须定义环境变量。如果您调用的变量没有定义,那么您将得到未定义的变量