简体   繁体   English

仅在添加全局导航保护后,Vue路由器才会引发“无法读取未定义的属性'匹配'”错误

[英]Vue-router throwing “Cannot read property 'matched' of undefined” error only after adding global navigation guards

After finally figuring out logging in and the first step of user-role-based routing, I've moved on to putting in place some authentication guards on various URLs. 在最终确定了登录和基于用户角色的路由的第一步之后,我开始着手在各种URL上放置一些身份验证保护措施。

It seemed simple enough, but I've run into an error and can't seem to find an example with a similar use case. 看起来很简单,但是我遇到了一个错误,而且似乎找不到具有类似用例的示例。 Everyone else I've seen who has this error seems to have misnamed their router instance as something other than "router". 我见过的其他所有出现此错误的人似乎都将其路由器实例错误地命名为“ router”以外的名称。 I have not, as far as I can tell. 据我所知,我还没有。 I'm using the vue-cli template with webpack, fyi. 我正在将vue-cli模板与webpack一起使用。

From my index.js: 从我的index.js:

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/trucker',
      component: Trucker,
      meta: { requiresAuth: true, truckerAuth : true, dispatchAuth: false },
      children: [
                {
                  path: '/loads',
                  component: Loads
                }
            ]
    },
    {
      path: '/dispatch',
      component: Dispatch,
      meta: { requiresAuth: true, truckerAuth : false, dispatchAuth: true },
      children: [
                {
                  path: '/drivers',
                  component: Drivers
                }
                ]
    },

  ]
})

router.beforeEach((to, from, next) => {
  if(to.meta.requiresAuth) {
    const employeeId = window.localStorage.getItem('employee_id')
    if(!employeeId) {
      next('/login')
    }
    else if(to.meta.truckerAuth) {
    const employeeId = window.localStorage.getItem('employee_id')
    if(employeeId === 3) {
      next()
    }else {
      next('/login')
    }
  } else if(to.meta.dispatchAuth) {
    const employeeId = window.localStorage.getItem('employee_id')
    if(employeeId === 2) {
      next()
    }else {
      next('/login')
    }
  }
  }else {
  next()
  }
})

From my main.js: 从我的main.js中:

import router from './router'

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

Any clues as to what is causing this error? 关于什么导致此错误的任何线索?

Update: I went back through and removed things until it worked again-- it turns out that changing my previous export default new Router syntax to be const router = new Router (along with changing plain "Router" to be VueRouter, to match examples, just in case) is actually what is causing this error. 更新:我反复检查并删除了所有内容,直到它再次起作用为止–事实证明,将以前的export default new Router语法更改为const router = new Router (以及将普通“ Router”更改为VueRouter,以匹配示例,以防万一)实际上是导致此错误的原因。 Still not sure why that would happen though. 仍然不确定为什么会这样。

Realized that I just needed to move my navigation guard into the main.js file, not have it in the index.js with the router. 意识到我只需要将导航保护移到main.js文件中,而不必将其与路由器一起放在index.js中。

So the corrected files look like: 因此,更正后的文件如下所示:

index.js: index.js:

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/trucker',
      component: Trucker,
      meta: { requiresAuth: true, truckerAuth : true, dispatchAuth: false },
      children: [
                {
                  path: '/loads',
                  component: Loads
                }
            ]
    },
    {
      path: '/dispatch',
      component: Dispatch,
      meta: { requiresAuth: true, truckerAuth : false, dispatchAuth: true },
      children: [
                {
                  path: '/drivers',
                  component: Drivers,
                  children: [
                      {
                        path: 'calendar',
                        component: Calendar
                      }
                  ]
                }
                ]
    },

  ]
})

main.js: main.js:

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
router.beforeEach((to, from, next) => {
  if(to.meta.requiresAuth) {
    const employeeId = window.localStorage.getItem('employee_id')
    if(employeeId == null) {
      next('/login')
    }
    else if(to.meta.truckerAuth) {
    const employeeId = window.localStorage.getItem('employee_id')
    console.log(employeeId)
    if(employeeId === 3) {
      next('/trucker')
    }else {
      next('/')
      console.log(employeeId)
    }
  } else if(to.meta.dispatchAuth) {
    const employeeId = window.localStorage.getItem('employee_id')
    if(employeeId === 2) {
      next()
    }else {
      next('/')
    }
  }
  }else {
  next()
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM