First, axios and CancelTokenSource are introduced
import axios, { CancelTokenSource } from 'axios';
Copy the code
Gets the CancelToken instance
const { CancelToken } = axios;
Copy the code
Get source instance
// source? : CancelTokenSource; const source = CancelTokenSource;Copy the code
Determines whether the request instance exists
/ / axios support request, details please refer to https://github.com/axios/axios#cancellation / / cancel the outstanding requests, If (source) {// Cancel the request and reset the status source.cancel(); source = null; }Copy the code
Access token
source = CancelToken.source();
const cancelToken = source.token;
Copy the code
request
Axios ({url:', method:'post/get', data: params, // withCredentials: true, // Allow cross-domain cookie information header: {' content-type ': 'application/json', // or 'Content-type ': 'application/x-www-form-urlencoded',}, cancelToken,})Copy the code
Let’s see what happens at the end