/*
* 统一管理axios的错误拦截
*
* */
import axios from 'axios'
import Vue from 'vue'
// import VueAxios from "vue-axios";
axios.defaults.baseURL = '/api' // 根据跨域的不同方式去调整 ,反向代理 。 如果是cors跨域就配置环境变量
// 项目中最好设置一个请求超时
axios.defaults.timeout = 5000;
// 接口错误拦截
axios.interceptors.response.use((response) => {
const res = response.data;
if (res.status == 0) {
return res.data
} else if (res.status == 10) { // 未登录的状态码 ,和后端统一
window.location.href = "/#/login"; // 拦截跳转 hash模式 , main.js中用router跳转没用,所以用window
} else {
alert(res.msg)
}
});
// Vue.use(VueAxios, axios)
// vue-axios 能自动将axios对象挂载到Vue实例上 = vue.prototype.$axios = axios
Vue.prototype.$axios = axios // 挂载到全局