1:首先在路由对象中的某一个具体的路由对象加这样一个属性vue
meta: { requireAuth:true }vuex
2:而后在main.js中添加这段代码ui
router.beforeEach((to, from, next) => {
if (to.meta.requireAuth) { // 判断该路由是否须要登陆权限
if (localStorage.getItem('a')) { // 经过vuex state获取当前的token是否存在,经过一个变量(vuex中或localstorage中),若是为真,那么跳转localstorage
next();
}else {
alert('请输入帐号和密码')
next({
path: '/',//若是为假,则重定向到这个路由路劲
query: {redirect: to.fullPath} // 将跳转的路由path做为参数,登陆成功后跳转到该路由
})
}
}else {
next()
}
})router
结束!!!这样就能在跳转以前拦截并作一些操做。对象