What is the interceptor:
Axios interceptor is sent to the server request and response back experienced two pass
Request interceptor:
axios every start , when requested to perform logic here, you can make a pre-configured to send axios in this place, you can also make inspections before the hair, in the case of checks ok started thinking the server send request
1 // request interceptor 2 axios.interceptors.request.use ( function (config) { . 3 // place business logic code . 4 return config; . 5 }, function (error) { . 6 // Axios occurred error handling . 7 return Promise .reject (error); . 8 });
In response interceptor:
axios completed with the server back to the client after the interaction to perform this logic, in this place can do some follow-up issues finishing, e.g. axios determines whether the request is successful, data filtering, or
// response blocker axios.interceptors.response.use ( function (Response) { // place business logic code // Response is returned to the server data, the data obtained is consistent with Promise return Response; }, function (error) { // Axios requests the server error handling occurs return Promise.reject (error); });
Interceptor details Description:
- config parameters
- config is an object axios. rather defaults (not equal)
- config for example, information may be configured to axios baseURL
- response parameters
- Specific data server returns information, consistent with the received data traffic axios
- Promise.reject()
- Syntactic sugar usage, the object returns a Promise