into-piece / Step-By-Step

每天一题向前端架构师前进
4 stars 1 forks source link

Axios #53

Open into-piece opened 3 years ago

into-piece commented 3 years ago
  1. Axios 如何缓存请求数据?
  2. 如何取消请求
  3. 拦截器interceptors 实现 token携带认证信息
into-piece commented 3 years ago

取消请求

cancelToken 底部原理是xmlHttpRequest 的abort

into-piece commented 3 years ago

interceptors

拦截器有对应request和response的设置,request设置发起的请求统一加上token的自定义header

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  config.headers.token = 'added by interceptor';
  return config;
});

// 添加响应拦截器
axios.interceptors.response.use(function (data) {
  data.data = data.data + ' - modified by interceptor';
  return data;
});