-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathAxios.js
41 lines (30 loc) · 1.03 KB
/
Axios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
# 安装
npm install axios -D //保存到依赖里
# 项目引入
import axios from 'axios';
var axios = require('axios');
创建实例 axios.create([config])
// 添加请求拦截器 所有请求前都在这里
axios.interceptors.request.use(function (config) {
// 在发送请求之前做些什么 例如loading
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器 所有请求响应后都在这里
axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});
如果你想在稍后移除拦截器,可以这样:
可以为自定义 axios 实例添加拦截器
const instance = axios.create();
instance.interceptors.request.use(function () {/\*...\*/});
*/
const myInterceptor = axios.interceptors.request.use(function () {/\*...\*/});
axios.interceptors.request.eject(myInterceptor);