vue常用之axios“接口的错误拦截”
import Vue from 'vue'import App from './App.vue'import router from "./router";//如果是routersimport axios from 'axios'import Vuelazyload from 'vue-lazyload'import VueCookie from 'vue-cookie'import store
·
import Vue from 'vue'
import App from './App.vue'
import router from "./router";//如果是routers
import axios from 'axios'
import Vuelazyload from 'vue-lazyload'
import VueCookie from 'vue-cookie'
import store from './store';
// import env from './env'
//1.建mock文件夹 api.js
const mock = true;//2.定义开关
if(mock){
require('./mock/api')
}
//根据前端的跨域方式做调整
axios.defaults.baseURL ='/api';//这里是接口代理,如果不是的话要换另一种
// axios.defaults.baseURL =env.baseURL;//这里是jsonp或cors跨域
axios.defaults.timeout = 8000;//操作时间8秒
//拦截器interceptors.response.use对错误进行处理,错误拦截
//用户的请求拦截是interceptors.request.use
axios.interceptors.response.use(function (response){
let res = response.data;
let path = location.hash;
console.log(res)
if(res.status == 0){//0成功
return res.data;
}else if (res.status == 10){//未登录
if(path != '#/login'){
window.location.href = '/#/login'
}
}else{
alert(res.msg);
return Promise.reject(res);//抛出异常
}
})
//载入懒加载
Vue.use(Vuelazyload,{
loading:'/imgs/loading-svg/loading-bars.svg',
})
//载入cookie
Vue.use(VueCookie)
Vue.prototype.$axios = axios
// Vue2.0建议使用Vue.prototype.$axios = axios;来进行挂载,不需要使用VueAxios插件了
Vue.config.productionTip = false
new Vue({
store,
router,
// router:routers,
render: h => h(App),
}).$mount('#app')
更多推荐
已为社区贡献2条内容
所有评论(0)