主要方法: to:进入到哪个路由去 from:从哪个路由离开 next:路由的控制参数,常用的有next(true)和next(false)

首先判断进入的是否是login页面?然后再判断是否已经登陆? 已经登陆了就进入你要跳转的页面,没登录就进入login页面

import VueRouter from 'vue-router'
import util from '../utils/util'

// 重复点击路由判断
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

// 判断是否登录
router.beforeEach((to, from, next) => {
  const isToken = util.cookies.get('token');
  if (to.name == 'login') {
    if (isToken) {
      next({ name: 'index' })
    } else {
      next()
    }
  } else {
    if (isToken) {
      next()
    } else {
      next({ name: 'login' });
    }
  }

})
Last Updated:
Contributors: pengrengui