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

路由跳转前,能够用导航守卫判断是否登陆,若是登陆了就正常跳转,没有登陆就把路由指向登陆页面。vue

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

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

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

  //能够在这里面判断是否登陆了router

  

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

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

        } 模板

 

}class

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

        if (auth.IsLogin) {  

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

       }  

    }  

  next();//必定要加上,否则不解析模板

}) 

to:即将进入的路由

from:即将离开的路由

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

相关文章
相关标签/搜索