You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 lines
782 B

5 years ago
# Axios拦截器
拦截器的用处就是拦截每一次的请求和响应,然后做一些全局的处理。
例如接口响应报错,可以在拦截器里用统一的报错提示来展示,方便业务开发。
本框架提供了一份拦截器参考代码 `src/api/index.js` ,因为每个公司提供的接口标准不同,所以该文件需要开发者根据各自公司的接口去定制对应的拦截器。
代码很简单,首先初始化 `axios` 对象,然后 `axios.interceptors.request.use()``axios.interceptors.response.use()` 就分别是请求和响应的拦截代码了。
参考代码里只做了简单的拦截处理,例如请求的时候会自动带上 `token` ,响应的时候会根据错误信息判断是登录失效还是接口报错。