vue 导航守卫,验证是否登录

路由跳转前,可以用导航守卫判断是否登录,如果登录了就正常跳转,没有登录就把路由指向登录页面。

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

const nextRoute = [ 'api', 'my/index, 'course'];  

  if(nextRoute.indexOf(to.name) >= 0){

  //可以在这里面判断是否登录了

  

        if (!store.state.auth.IsLogin) {  

           vueRouter.push({name: 'login'})  

        } 

 

}

if (to.name === 'login') {  

        if (auth.IsLogin) {  

            vueRouter.push({name: 'home'});  

       }  

    }  

  next();//一定要加上,不然不解析模板

}) 

to:即将进入的路由

from:即将离开的路由

next:是一个函数,一定要加上,不然不解析模板

 

原文地址:https://www.cnblogs.com/liubu/p/9050373.html