axios.create
时间: 2023-09-06 10:10:15 浏览: 88
Axios is a popular JavaScript library used for making HTTP requests from client-side applications. The `axios.create()` method is used to create a new Axios instance with a custom configuration.
Here's an example of how to use `axios.create()`:
```js
import axios from 'axios';
const instance = axios.create({
baseURL: 'https://api.example.com',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer YOUR_TOKEN_HERE'
}
});
instance.get('/users')
.then(response => console.log(response.data))
.catch(error => console.error(error));
```
In this example, we create a new Axios instance with a base URL of `https://api.example.com` and set some default headers that will be included with every request made with this instance. We then use this instance to make a GET request to `/users`, and log the response data to the console if the request is successful, or log the error to the console if the request fails.
Using `axios.create()` can be useful if you need to make multiple requests to the same API endpoint with the same configuration or headers. By creating a new instance with a custom configuration, you can avoid having to repeat the same configuration options in each request.
阅读全文