Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue - Vue CLI - API Variables

What is the best way to set api url's based on current domain location within the Vue Instance? In angular I would be using something similar to Angular Environment However I have not come accross anything similar with Vue or Axios.

In this case my setup is dependent on multiple environments ( local, dev, stage, prod ). All based on domain relationship.

localhost = '/api-call'

dev.domain.com = 'devapi.domain.com'

prod.domain.com = 'prodapi.domain.com'

Any help at all would be incredible!

like image 373
Brock Avatar asked Feb 04 '26 03:02

Brock


1 Answers

Oftentimes what I would do is store it on the backend in the config file and then set the variable in axios, but you could also test the browser URL on the front-end and set axios accordingly. When creating the axios instance you would set the baseURL property to your main site url.

var instance = axios.create({
      baseURL: 'https://' + window.location.host + '/api/',
      timeout: 1000,
      headers: {'X-Custom-Header': 'foobar'}
});
like image 168
Miguel Coder Avatar answered Feb 06 '26 16:02

Miguel Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!