vue中如何实现前置守卫

可以使用 router.beforeEach 注册一个全局前置守卫:

//前置守卫
router.beforeEach((to, from, next) => {
    
    //路由跳转中
    if (to.path == "/") {
    
    
        next();
    } else {
    
    
        if (window.localStorage.getItem("loginUserInfo")) {
    
    
            next()
        } else {
    
    
            next({
    
    path: '/'})
        }
    }
})

猜你喜欢

转载自blog.csdn.net/fangqi20170515/article/details/126699625