Change the default base url for axios

前端 未结 4 1289
耶瑟儿~
耶瑟儿~ 2021-02-01 12:42

I have configured my axios like this

const axiosConfig = {
  baseURL: \'http://127.0.0.1:8000/api\',
  timeout: 30000,
};

Vue.prototype.$axios = axios.create(ax         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 13:19

    1. Create .env.development, .env.production files if not exists and add there your API endpoint, for example: VUE_APP_API_ENDPOINT ='http://localtest.me:8000'
    2. In main.js file, add this line after imports: axios.defaults.baseURL = process.env.VUE_APP_API_ENDPOINT

    And that's it. Axios default base Url is replaced with build mode specific API endpoint. If you need specific baseURL for specific request, do it like this:

    this.$axios({ url: 'items', baseURL: 'http://new-url.com' })
    

提交回复
热议问题