Uncaught TypeError: Cannot read properties of undefined (reading ‘beforeEach‘)

问题描述

运行项目页面一片空白,报错

Uncaught TypeError: Cannot read properties of undefined (reading ‘beforeEach‘)_第1张图片


原因和解决方案:

router.beforeEach写在定义router之后

代码

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path !== '/null') {
    if (localStorage.getItem("token")) {
      next()
    } else {
      next("/null")
    }
  } else {
    next()
  }
})

你可能感兴趣的:(Bugs,node.js,vue)