温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • VUE设置控制守卫,未登录时自动跳转到登录页,登录后正常访问

VUE设置控制守卫,未登录时自动跳转到登录页,登录后正常访问

发布时间:2020-02-26 10:14:01 来源:网络 阅读:283 作者:o凤舞九天o 栏目:开发技术

index.js中设置如下内容:


const router = new Router({

  routes: [

    {

      path: '/',

      name: 'HelloWorld',

      component: HelloWorld,

      meta:{requireAuth:true}

    },

    {

      path: '/login',

      name: 'login',

      component: Login

    },

    {

      path: '/index',

      name: 'index',

      component: index,

      meta:{requireAuth:true}

    },

  ]

});


router.beforeEach((to, from, next) => {

 //requireAuth对应上面每个跳转的 meta:{requireAuth:true},

 //配置上说明访问该路径时需要检测是否登录状态

 if(to.meta.requireAuth) {

   //sessionStorage可直接使用,无需引入

   //在登录页,点击登录按钮后设置sessionStorage.setItem("key","value")

   //通过sessionStorage.getItem("account")获取,如果有值则是登录状态,无值则为未登录,自动跳转到登录页

   if(sessionStorage.getItem("account") == null) {  

      next('/login');      

   }else{     

     next();

    }

 }

 else{

    next();

 }

});



export default router;


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI