Dynamic host in axios

。_饼干妹妹 提交于 2019-12-01 14:12:21

You probably do not need to set baseURL. Have you tried to define baseURL each time you make requests? For example,

axios.get(`${host}:${port}/api/categories`)

Or, depending on your words "The goal is to get the right host depending on the environment.", you may define proper host using your environment variables, for example:

axios.get(`${proccess.env.HOST}:${PORT}/api/categories`)

If you use a bundler for your frontend code, this expression will be resolved to

axios.get('http://example.com:80/api/categories')

That's because your bundler actually should run with pre-defined environment variables HOST and PORT

I found a solution. Add this code in your: /src/main.js

Example:

const baseURL = 'http://localhost:8080';
if (typeof baseURL !== 'undefined') {
  Vue.axios.defaults.baseURL = baseURL;
}

The answer that helped me in this: Set baseURL from .env in VueAxios

Thanks everyone for help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!