美文网首页
简单的路由守卫

简单的路由守卫

作者: 兰德索尔 | 来源:发表于2021-04-23 19:23 被阅读0次

    需要在配置路由时给路由添加meta,如下:

    component:'组件',
    meta:{
      isLogin:true
    }
    
    // 路由守卫
    router.beforeEach((to, from, next) => {
        // console.log(to)
        if (to.matched.some(res => res.meta.isLogin)) { //判断是否需要登录
            if (jsCookie.get('AdminToken')) {
                next();
            } else {
                next({
                    path: "/",
                    query: {
                        redirect: to.fullPath
                    }
                });
            }
        } else {
            next()
        }
    });
    

    相关文章

      网友评论

          本文标题:简单的路由守卫

          本文链接:https://www.haomeiwen.com/subject/sattrltx.html