[Vuejs]-Axios base url from local json file

0👍

You could try with .env files https://cli.vuejs.org/guide/mode-and-env.html

0👍

If you’re using vue-cli, then this would work for you:

Inside main.js file (this is entry point):

import axios from 'axios'

/**
 * config.json implementation
 */
let axiosInstance = null

;(async () => {
  try {
    const config = await axios.get('/config.json')
    axiosInstance = axios.create({
      baseURL: config?.data?.url || 'some-fallback-url'
    })
  } catch (err) {
    console.warn('Error!', err)
  } finally {
    Vue.prototype.$http = axiosInstance

    new Vue({
      router,
      store,
      render: h => h(App),
    }).$mount('#app')
  }
})()

Leave a comment