【问题标题】:Change the default base url for axios更改 axios 的默认基本 url
【发布时间】:2018-05-04 14:09:44
【问题描述】:

我已经像这样配置了我的 axios

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

Vue.prototype.$axios = axios.create(axiosConfig)

在我的组件中,我拨打电话

this.$axios.get('items').then()..

现在上述方法有效,但我想在不影响全局基本 URL 的情况下更改 baseURL,以便在我的组件中我可以在没有 API 端点的情况下简单地使用它

我试过了

this.$axios.baseURL = "http://127.0.0.1:8000";
this.$axios.get().. //this is still in api endpoint

我该怎么做?

【问题讨论】:

  • 你可以通过设置this.$axios.defaults.baseURL = "https://localhost:8000"来改变axios实例的base url
  • 对于其他访问者,要临时更改baseURL,您可以将绝对url传递给所需的方法:this.$axios.get('http://127.0.0.1:8000/items')
  • axios.get('/items', { baseUrl: 'https://another-endpoint' } 或另一个示例 axios.post('/items', payload, { baseUrl: 'https://another-endpoint' }) 是另一种选择。请参阅github.com/axios/axios#config-order-of-precedence 了解配置优先顺序。
  • 那么用baseURL创建实例有什么用呢? axios.create(axiosConfig)

标签: javascript vue.js vuejs2 axios


【解决方案1】:

代替

this.$axios.get('items')

使用

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

如果不传递method: 'XXX',那么默认通过get方法发送。

请求配置: https://github.com/axios/axios#request-config

【讨论】:

  • 对于临时更改 baseURL,您可以将绝对 url 传递给所需的方法:this.$axios.get('http://new-url.com/items')。无需将所有参数作为对象传递给this.$axios
  • 虽然这会更改 baseURL,但对于所有构建模式都是相同的。 + 它仅针对这个特定的 axios 请求更改 baseUrl。如何正确并以 VUE 方式完成,请查看我的答案。
【解决方案2】:

把我的两分钱放在这里。我想在不为我的特定请求硬编码 URL 的情况下做同样的事情。所以我想出了这个解决方案。

要将'api' 附加到我的baseURL,我将我的默认baseURL 设置为,

axios.defaults.baseURL = '/api/';

然后在我的具体请求中,明确设置方法和url后,我将baseURL设置为'/'

axios({
    method:'post',
    url:'logout',
    baseURL: '/',
   })
   .then(response => {
      window.location.reload();
   })
   .catch(error => {
       console.log(error);
   });

【讨论】:

  • 所以如果你没有在你的方法中设置baseUrl,它就不能使用相对路径?如果我输入像http://server01.domain.com 这样的明确网址,我可以让baseUrl 工作,但我必须在多个服务器上设置应用程序,每个服务器都是独立的。
  • @Jack 我正在处理你同样的情况,你找到一个优雅的解决方法了吗?
  • @MatiasSalimbene 我将全局设置为/MyApp/api,但没有在方法中设置它。然后将路由器设置为/MyApp
【解决方案3】:
  1. 创建 .env.development.env.production 文件(如果不存在)并在其中添加您的 API 端点,例如:VUE_APP_API_ENDPOINT ='http://localtest.me:8000'
  2. 在 main.js 文件中,在导入后添加这一行:axios.defaults.baseURL = process.env.VUE_APP_API_ENDPOINT

就是这样。 Axios 默认基本 URL 被替换为构建模式特定的 API 端点。 如果您需要针对特定​​请求的特定 baseURL,请执行以下操作:

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

【讨论】:

    【解决方案4】:

    如果其他人仍然需要帮助:

    你可以直接在你想使用的地方更改 Axios 基础 URL

    anotherAxiosRequest(){
     axios.defaults.baseURL = http://wwww.example.com
     axios({
       url:'/cats' //=>  http://wwww.example.com/cats
     })
    }
    

    它会更改基本 URL

    请注意,这不会更改 main.js 中定义的基本 URL

    【讨论】:

      【解决方案5】:

      axios docs 你有 baseURLurl

      baseURL 将在发出请求时添加到url。因此您可以将baseURL 定义为http://127.0.0.1:8000 并向/url 提出请求

       // `url` is the server URL that will be used for the request
       url: '/user',
      
       // `baseURL` will be prepended to `url` unless `url` is absolute.
       // It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
       // to methods of that instance.
       baseURL: 'https://some-domain.com/api/',
      
      

      【讨论】:

      • 我已经阅读了那些 Axios 文档,但奇怪的是我的 baseURL 从来没有被放在前面。如果我只是假装baseURL 功能不存在并适当地更改我的url,那么一切正常。
      【解决方案6】:

      你也可以用这个 (适用于 nuxt 和 vue)

      this.$axios.defaults.baseURL = 'http://example.com/example'
      

      这对我来说适用于一个请求的自定义 baseURL

      async getDataFromServer({ commit }, payload) {
          commit('setLoadingStatus', true)
          try {
            const page = payload.page || 1
            const sort = payload.sort || 'published_at'
      
            this.$axios.defaults.baseURL = 'http://example.com/example'
           
            const { data } = await this.$axios.get(`/example`)
      
            commit('setItOnState', data)
          } catch (e) {
            throw new Error(e)
          } finally {
            commit('setLoadingStatus', false)
          }
        },
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2018-10-12
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      相关资源
      最近更新 更多