vue-router的beforeEach全局守卫

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    const role = localStorage.getItem('ms_username');
    if(!role && to.path !== '/login') {
        next("/login")
    } else if(role && to.path === "/login") {
        //登录后,在路由修改不能返回登录,必须点击退出可以返回登录页
        next(from.path)
    } else{
        next()
    }
});
发布了107 篇原创文章 · 获赞 23 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_42092177/article/details/104815180