Reactjs 如何在执行某些特定HTTP谓词后执行某些代码
我需要在每次PUT、补丁和POST之后执行一些代码。我不想在每次axios调用中都在得到响应后执行我的代码;我希望在一个地方声明代码,以确保为每个PUT/POST/PATCH触发其执行。我想到了axios response拦截器,但在返回response之前,您需要做一些事情,而在我的例子中,我希望返回对ST的响应,然后我将执行我的代码。签出Reactjs 如何在执行某些特定HTTP谓词后执行某些代码,reactjs,axios,Reactjs,Axios,我需要在每次PUT、补丁和POST之后执行一些代码。我不想在每次axios调用中都在得到响应后执行我的代码;我希望在一个地方声明代码,以确保为每个PUT/POST/PATCH触发其执行。我想到了axios response拦截器,但在返回response之前,您需要做一些事情,而在我的例子中,我希望返回对ST的响应,然后我将执行我的代码。签出 import axios from 'axios'; import { Service } from 'axios-middleware'; const
import axios from 'axios';
import { Service } from 'axios-middleware';
const service = new Service(axios);
service.register({
onRequest(config) {
console.log('onRequest');
//EDIT YOUR REQUEST CONFIG
return config;
},
onResponse(response) {
console.log('onResponse');
//EDIT YOUR RESPONSE
return response;
}
});
axios('https://jsonplaceholder.typicode.com/posts/1')
.then(({ data }) => console.log('Received:', data));