vue-router 判断是否登陆,未登录跳转登陆页面

移动app 只需验证首页是否登陆

meta: {
  title: 'index',
  requireAuth: true
}

main.js中

router.beforeEach((to, from, next) => {
  if (to.matched.some(m => m.meta.requireAuth)) {
    console.log(store.getters.token)
    // 对路由进行验证
    if (store.getters.token) { // 已经登陆
      next() // 正常跳转到你设置好的页面
    } else {
      // 未登录则跳转到登陆界面
      next({ path: '/login' })
    }
  } else {
    next()
  }
})

猜你喜欢

转载自blog.csdn.net/duanran0/article/details/86354375