axiosAPI中文版
https://blog.lee-cloud.xyz/post/1/Axios-zhong-wen-wen-dang
axios.create :创建一个拥有通用配置的axios实例
axios.creat([config])
例子:
var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
建造拦截器
axios.interceptors.request.use(function(config){ })
axios.interceptors.response.use(function(config){ })
列子:
// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
// Do something with response error
return Promise.reject(error);
});
你也可以为axios实例添加一个拦截器
var instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});
可以通过导入相关配置发起请求
axios(config)
// 发起一个POST请求
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});