博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vue 路由 导航守卫(全局守卫、路由独享守卫)
阅读量:2497 次
发布时间:2019-05-11

本文共 1225 字,大约阅读时间需要 4 分钟。

一.全局守卫

回调函数中的参数,to:进入到哪个路由去,from:从哪个路由离开,next:函数,决定是否展示你要看到的路由页面。main.js中设置全局守卫

router.beforeEach((to, from, next) => {  let isLogin = localStorage.getItem('isLogin')  if (to.matched.length === 0) { //没有匹配到当前路由    next('/error')  } else if (!isLogin && to.path !== '/login') {    //如果没有登录,跳转到登录页面    next('/login')  } else {    if ((to.path === '/login' || to.path === '/error') && isLogin) {      //如果已经登录,却尝试访问登录页面或者错误页面,将继续保持原本的页面       next('check')    } else {      next()    }    // next()  }  // next()})

二.路由独享的守卫

beforeEnter:(to,from,next)=>{},用法与全局守卫一致。只是,将其写进其中一个路由对象中,只在这个路由下起作用。

{      path: '/login',      name: 'login',      component: Login,      beforeEnter(to,from,next){        console.log(to)          let isLogin = localStorage.getItem('isLogin')          if (to.matched.length === 0) { //没有匹配到当前路由            next('/error')          } else if (!isLogin && to.path !== '/login') {            //如果没有登录,跳转到登录页面            next('/login')          } else {            if ((to.path === '/login' || to.path === '/error') && isLogin) {              //如果已经登录,却尝试访问登录页面或者错误页面,将继续保持原本的页面               next('/check')            } else {              next()            }            // next()          }      }    },

 

转载地址:http://aelrb.baihongyu.com/

你可能感兴趣的文章
设计模式11_装饰器
查看>>
设计模式12_外观模式
查看>>
设计模式13_享元模式
查看>>
设计模式14_组合结构
查看>>
设计模式15_模板
查看>>
海龟交易法则01_玩风险的交易者
查看>>
CTA策略02_boll
查看>>
vnpy通过jqdatasdk初始化实时数据及历史数据下载
查看>>
设计模式19_状态
查看>>
设计模式20_观察者
查看>>
vnpy学习10_常见坑02
查看>>
用时三个月,终于把所有的Python库全部整理了!拿去别客气!
查看>>
pd.stats.ols.MovingOLS以及替代
查看>>
vnpy学习11_增加测试评估指标
查看>>
资金流入流出计算方法
查看>>
海龟交易法则07_如何衡量风险
查看>>
海龟交易法则08_风险与资金管理
查看>>
海龟交易法则09_海龟式积木
查看>>
海龟交易法则10_通用积木
查看>>
海龟交易法则14_掌控心魔
查看>>