代码之家  ›  专栏  ›  技术社区  ›  gyc

VueJS和Axios prod env的多个基本url

  •  0
  • gyc  · 技术社区  · 8 年前

    要在prod和dev env中配置我的应用程序,我目前使用如下方法:

    let baseURL;
    
    if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
      baseURL = 'http://127.0.0.1/';
    } else {
      baseURL = 'http://api.example.com';
    }
    
    export const HTTP = axios.create(
      {
        baseURL: baseURL,
        headers: {...}
      })
    

    然后我用

    HTTP.get(...).then(...)
    

    例如,我需要做:

    HTTP.userApi.get()
    HTTP.productApi.get()
    

    1 回复  |  直到 8 年前
        1
  •  3
  •   Andrey Popov    8 年前

    在以下方面有两种选择: axios

    1. custom instance
    // Create an instance using the config defaults provided by the library
    // At this point the timeout config value is `0` as is the default for the library
    const instance = axios.create();
    
    // set a default baseURL for this particular instance
    instance.defaults.baseURL= 'http://new-root.com';
    
    1. 使用自定义 baseURL 使用 request config :
    axios.get('my/url', { baseURL: 'http://new-root.com' })
    

    轴心 每个“端点”的实例- HTTP.userApi 将是实例和 HTTP.productApi

    祝你好运!

    推荐文章