vue axios 封装
Posted rosendolu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue axios 封装相关的知识,希望对你有一定的参考价值。
import Cookies from 'js-cookies';
import axios from 'axios';
/**
* 封装,拦截
*/
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers['token'] = 'token';
axios.defaults.headers.post['Content-Type'] = 'application/json';
const Axios = axios.create(
baseURL: 'https://some-domain.com/api/',
timeout: 10000,
headers:
'X-Custom-Header': 'foobar',
'token':'token'
);
Axios.interceptors.request.use(config =>
//
if(true)
config.headers['from'] = 'web'
return config;
else
return config;
,error =>
return Promise.reject(error);
)
Axios.interceptors.response.use(res =>
// res schema
// res.status
// res.data
// res.statusText
// res.headers
// res.config
// res.request
if(res.status = 200)
return res.data;
,error =>
// 错误处理
const code = error.response.status;
switch (code )
case code > 500:
// toast('server:error')
break;
default:
break;
return Promise.reject(error)
)
const get = (path,params) =>
return Axios.get(path,params: params)
const post = (path,params) =>
return Axios.post(path,params)
// concurrent request
const all = (...args) =>
return axios.all(args)
export get,post,all
references
以上是关于vue axios 封装的主要内容,如果未能解决你的问题,请参考以下文章