vue2路由攔截


這是一個剪刀的路由攔截

router分位兩個文件,一個是路由統一配置,一個是具體的路由信息

1路由攔截

import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './index.config'
import { routerList } from '@/common/js/url-white-list'
Vue.use(VueRouter)

const router = new VueRouter({
mode: 'hash',
// mode: 'history',
base: __dirname,
routes,
//在頂部點擊當前路由菜單回到頂部
scrollBehavior() {
return {
x: 0,
y: 0
}
/* 跳轉頁面返回頂部 */
}
})
 
router.beforeEach((to, from, next) => { //簡單的判斷

// 百度統計,上報數據
if (window._hmt) {
if (to.path) {
// eslint-disable-next-line
window._hmt.push(['_trackPageview', '/#' + to.fullPath]);
}
}
let roleType = window.localStorage.getItem('roleType');   //用戶角色
// let url = window.location.hash ? window.location.hash.split('#')[1] : 
const token = window.localStorage.getItem("access_token")//是否登錄
let openid = window.localStorage.getItem("openid");
if (routerList.indexOf(to.path) === -1) { //不需要登錄后回來的地址 比如修改密碼頁面
window.localStorage.setItem('redirectUrl', to.path);   //記錄未登錄之前從哪個菜單點擊的登錄的,登錄成功之后跳轉到這個頁面
if (!to.meta.requireAuth && !token && !openid) {   //如果用戶未登錄沒有openid又不需要用戶登錄的頁面 比如微官網的頁面 到登錄頁面之后進入beforeRouteEnter之后用戶未綁定登錄關系需要返回的頁面
window.localStorage.setItem('landingUrl', to.path);
}
}
if ((to.meta.requireAuth || !openid) && routerList.indexOf(to.path) === -1) {
if (!token) {
document.title = "登錄";
next({
path: '/login',
});
} else if (roleType && to.meta.role.indexOf(roleType) === -1) {   //沒權限
if (to.meta.requireRole) { //暫無權限
document.title = "能源e+";
next({
path: '/jurisd',
});
// Toast({ message: '您沒有訪問權限', duration: 1000 })
}
} else {
document.title = to.meta.title;
next();
}
} else {
document.title = to.meta.title;
next();
}

});

export default router


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM