const routes = [
{//入口
path: ‘/‘,
redirect: ‘/login’,
component: () => import( ‘@/views/Login/index.vue’ ),
meta: {title:’’}
},
]
const whiteList = [‘/login’] //校验白名单
router.beforeEach((to, from, next) => {
console.log(to.path)
if (whiteList.indexOf(to.path) !== -1) {
//将要去的路由是否在白名单,如果在白名单就往下执行,如果不在白名单则需要校验后执行
next()
} else {
const user = get(‘userInfo’)
if (user) {
next()
} else {
if (to.path === ‘/login’) {
next()
} else {
next(‘/login’)
}
}
}
})
function save(name, data) {
sessionStorage.setItem(name, JSON.stringify(data))
}
function get(name) {
return JSON.parse(sessionStorage.getItem(name))
}