One, the basic use of axios
1.1 Install axios
Excuting an order:npm install axios --save
1.2 Send a get request demo
1.3 Send concurrent requests
Sometimes, we may need to send two requests at the same time
- Using axios.all, you can put multiple request arrays.
- The result returned by axios.all([]) is an array. Use axios.spread to expand the array [res1,res2] into res1, res2
1.4 Global configuration
In the above example, our BaseURL is fixed
- In fact, many parameters may be fixed during development.
- At this time, we can perform some extraction, or use the global configuration of axiox
1.5 Common configuration options
1.6 Examples of axios
Why create an instance of axios?
- When we import objects from the axios module, the instance used is the default instance.
- When some default configurations are set for this instance, these configurations are fixed.
- However, in subsequent development, some configurations may be different.
- For example, some requests need to use a specific baseURL or timeout or content-Type, etc.
- At this time, we can create a new instance and pass in the configuration information belonging to the instance.
1.7 axios package
The first way: the
second way: the
third way: the
fourth way (master):
1.8 How to use an interceptor?
- Axios provides an interceptor for us to perform corresponding processing after sending each request or getting the response.
- How to use an interceptor?