axios可以单独做请求拦截吗?

axios可以单独做请求拦截吗?

比如,我发送登录请求,接收请求前加一个单独的loading动画这样。

阅读 4.3k
2 个回答

可以使用 Interceptors

拿 loading 来讲,代码大概长这样:

axios.interceptors.request.use(function (config) {
  // 开启 loading
  startLoading()
  return config
});

axios.interceptors.response.use(function (response) {
  // 关闭 loading
  endLoading()
  return response
})

你可以在调用axios发送请求之前加载loading动画,请求结束后结束动画。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题