1.http.js
/**
*
* http配置
*/
import axios from ‘axios‘
import { Loading, Message } from ‘element-ui‘
// axios 配置
axios.defaults.timeout = 5000;
// http request 拦截器
axios.interceptors.request.use(
config => {
// if (store.state.token) {
// config.headers.Authorization = `token ${store.state.token}`;
// }
return config;
},
err => {
return Promise.reject(err);
});
// http response 拦截器
axios.interceptors.response.use(
response=> {
return response;
},
err => {
if (err && err.response) {
switch (err.response.status) {
case 400:
Message.error(‘请求错误‘)
break
case 401:
Message.error(‘未授权,请登录‘)
break
case 403:
Message.error(‘拒绝访问‘)
break
case 404:
Message.error(`请求地址出错: ${err.response.config.url}`)
break
case 408:
Message.error(‘请求超时‘)
break
case 500:
Message.error(‘服务器内部错误‘)
break
case 501:
Message.error(‘服务未实现‘)
break
case 502:
Message.error(‘网关错误‘)
break
case 503:
Message.error(‘服务不可用‘)
break
case 504:
Message.error(‘网关超时‘)
break
case 505:
Message.error(‘HTTP版本不受支持‘)
break
default:
}
}
return Promise.reject(err)
});
export default axios;
2.main.js
import axios from ‘./assets/js/http.js‘
Vue.prototype.$http = axios
原文地址:https://www.cnblogs.com/tmsdxcz/p/8954403.html