Vue+axios(interceptors) 实现http拦截 + router路由拦截 (双拦截)+ 请求自带loading效果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Vue+axios(interceptors) 实现http拦截 + router路由拦截 (双拦截)+ 请求自带loading效果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3202字,纯文字阅读大概需要5分钟。
内容图文
axios interceptors 拦截器
// interceptors.js // vue axios配置 发起请求加载loading请求结束关闭loading // http request 请求拦截器,有token值则配置上token值 import axios from‘axios‘ import router from‘../router‘ import { Loading } from‘element-ui‘ import Promise from‘promise‘var loadinginstace // http请求拦截器axios.interceptors.request.use( config => { // element ui Loading方法 loadinginstace = Loading.service({ lock: true, text: ‘Loading‘, spinner: ‘el-icon-loading‘, background: ‘rgba(0, 0, 0, 0.3)‘, customClass:"osloading", fullscreen: true }) return config }, error => { loadinginstace.close() return Promise.reject(error) } ) // http response 服务器响应拦截器, // 这里拦截401错误,并重新跳入登页重新获取tokenaxios.interceptors.response.use( response => { if(response.data.data && response.data.data.ecode == ‘401‘) { loadinginstace.close() router.replace({ path: ‘/login‘, // query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面 }) }else{ loadinginstace.close() return response; } }, error => { loadinginstace.close() if (error.response) { switch (error.response.status) { case401: // 这里写清除token的代码 router.replace({ path: ‘/login‘, // query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面 }) } } return Promise.reject(error.response.data) } ); export default axios;
路由拦截
// main.js import auth from‘@/api/auth‘ auth.refreshAuth(); router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requireAuth)){ // 判断该路由是否需要登录权限 let isLogin = auth.checkAuth() if(isLogin) { // 判断当前的token是否存在 next(); }else { next({ path: ‘/login‘, }) } } else { next(); } });
auth.js
// auth.js const auth = { user:{ authenticated: false }, checkAuth(){ returnthis.user.authenticated }, refreshAuth(){ let _token = sessionStorage.getItem(‘token‘); if (_token){ this.user.authenticated = true } else { this.user.authenticated = false } }, } export default auth;
router.js
import Vue from‘vue‘ import Router from‘vue-router‘ Vue.use(Router) const routes = [ { path:‘/login‘, name: ‘login‘, component:resolve => require([‘@/pages/custom/login‘],resolve) }, { path:‘/infAddition‘, name: ‘infAddition‘, component:resolve => require([‘@/pages/custom/infAddition‘],resolve) }, { path:‘/home‘, name: ‘home‘, component:resolve => require([‘@/pages/custom/home‘],resolve) }, { path:‘/‘, name: ‘mainframe‘, //根目录 重定向 redirect: { path: ‘/home‘, query: {schemaId: ‘973e-36c0d61b48a5‘}}, meta: { requireAuth: true}, component:resolve => require([‘@/pages/system/mainframe‘],resolve), children: [ { path: ‘/home‘, name:‘home‘, component: resolve => require([‘@/pages/custom/home.vue‘], resolve) }, ] }, ] const vueRouter = new Router({ base: __dirname , mode:"history",//启用浏览器的历史记录 // mode:"hash", scriollBehavior:()=>({ x: 0, y: 0 }), routes }) import auth from‘@/api/auth‘ vueRouter.beforeEach(function (to, from, next){ const nextRoute = [‘/‘,‘/home‘,‘/infAddition‘,‘/take‘,‘/submitted‘] const token = sessionStorage.getItem(‘token‘) if(nextRoute.indexOf(to.path) >= 0){ if(token == null || token == "" ){ next({path:‘/login‘}) }else{ next() } }else{ next() } }) export default vueRouter
原文:https://www.cnblogs.com/qdwz/p/10950889.html
内容总结
以上是互联网集市为您收集整理的Vue+axios(interceptors) 实现http拦截 + router路由拦截 (双拦截)+ 请求自带loading效果全部内容,希望文章能够帮你解决Vue+axios(interceptors) 实现http拦截 + router路由拦截 (双拦截)+ 请求自带loading效果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。