Axios one of the most commonly used features, interceptors
axios.interceptors.response.use ( = = = Response = JSON if return resp; Throw New Error (resp.msg) }, = = {return promise.reject (Error) } },)
After the data is returned in the background, the detection status code is 200, then the data is returned, otherwise, an exception is thrown
Another feature, cancel the request, the following from the official
var canceltoke = Axios. Canceltoken; var source = Canceltoken.source (); Axios.get ( '/user/12345 ' catch (function (thrown) { if (Axiso.iscancel (thrown)) {Console.log ( ' Rquest canceled ') Span style= "COLOR: #000000" >, thrown.message); else { // handle error }}); // Cancel the request (the information parameter set can be set) source.cancel ("Operation is canceled by user");
Or CancelToken
pass a executor function to the constructor to create a cancel token
var Canceltoken = Axios. Canceltoken; var cancel;axios.get ('/user/12345 ', { new Canceltoken (function Executor (c { // This executor function accepts a cancel function as a parameter , cancel = C; })}); // Cancel the request cancel ();
In most cases, the methods we encapsulate are exported through the export,
So cancel we can also export output, so that you can call the export {cancel} at any time in the main business
Axios Interceptor Cancellation request