vue登录

1:先在定义路由的时候就需要多添加一个自定义字段requireAuth,用于判断该路由的访问是否需要登录。如果用户已经登录,则顺利进入路由, 否则就进入登录页面。在路由管理页面添加meta字段

2:router/index.js文件,例如,在用户直接跳转/manage 路径下的时候,实现路由拦截

   {path:'/manage',

    name:'manage',

    component:manage, 

    meta:{requireAuth:true}

            },

3: 在main.js 中加上

router.beforeEach((to, from, next) => {
console.log(to);
console.log(from);
if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
if(localStorage.getItem('access_token')){ //判断本地是否存在access_token
next();
}else {
next({
path:'/'
})
}
}
else {
next();
}
/*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
if(to.fullPath == "/login"){
if(localStorage.getItem('access_token')){
next({
path:from.fullPath
});
}else {
next();
}
}
})
原文地址:https://www.cnblogs.com/cs122/p/10168749.html