Axios from nothing to catch (5) – default Axios and axios.create The difference between ()


As mentioned above, the default exposed Axios are created through the createinstance function. In development, in addition to using Axios, we can also use the axios.create () create a new Axios instance. What is the difference between the two objects?
First, paste the source code

// Create the default instance to be exported
var axios = createInstance(defaults);

// Expose Axios class to allow class inheritance
axios.Axios = Axios;

// Factory for creating new instances
axios.create = function create(instanceConfig) {
  return createInstance(mergeConfig(axios.defaults, instanceConfig));

// Expose Cancel & CancelToken
axios.Cancel = require('./cancel/Cancel');
axios.CancelToken = require('./cancel/CancelToken');
axios.isCancel = require('./cancel/isCancel');

// Expose all/spread
axios.all = function all(promises) {
  return Promise.all(promises);
axios.spread = require('./helpers/spread');

It can be determined that Axios is an object created by Axios library calling createInstance () by default, and instance is the Axios object we created manually by calling create function, and createInstance function is also called in create function.
Definition of mergeconfig():’function mergeconfig (config1, config2) {} ‘, generates a new configuration object. If the attributes in config1 and config2 conflict, the definition in config2 takes precedence

  1. Both of them are functions that can send requests, that is Axios.prototype.request ([config]) function
  2. Both of them have various ways to send specific requests on the Axios prototype object, get / post / put / delete
  3. They have their own default configuration and interceptor configuration


  1. The default configuration is likely to be different and used in the project axios.create () creating a new Axios is to distinguish it from the default Axios. If you pass in the same config, is it too much?
  2. In the source code, after the create function is defined, many attributes are added to the default Axios, such as cancel, canceltoken, all, etc. so it is certain that the instance generated from create() does not have these attributes added later
Axios series gate

Axios from scratch to catch (1) – encapsulating a simple ajax request function with XMLHttpRequest
Axios from nothing to catch (2) – understanding and use of Axios
Axios from scratch to catch (3) – the whole structure of source code analysis
Axios from scratch to catch (4) – the relationship between Axios and Axios in source code analysis
Axios from nothing to catch (5) – default Axios and axios.create The difference between ()
Axios from scratch to catch (6) – the whole process of Axios running in source code analysis