【问题标题】:router is undefined in vuejs路由器在 vuejs 中未定义
【发布时间】:2021-01-30 17:13:06
【问题描述】:

我创建了完成的 vue js 应用程序。其中我有路由的主 index.js 文件,我为其他视图制作了不同的路由文件,我的所有子文件都在主 index.js 路由文件中扩展。

index.js(主路由文件) 下面我在这个文件中导入我的子路由

从 './test1' 导入 test1 从'./test'导入测试

我的子路由文件test1

export default [{
  path: '/roles',
  component: Layout2,
  children: [{
    path: '/',
    component: () => import('@/views/test/test1'),
    meta: {
        auth: true
    },
    beforeEnter(to, from, next) {
        if(checkPermission("readUser")){
        router.push({
                name: 'unauthorized'
            })
        }
    }
  }
  }]
}]

现在的问题是我正在尝试通过使用之前的路由在 url 中推送 unauthorized,但它给了我错误,例如未定义路由器。我该如何解决这个问题?

【问题讨论】:

  • 是的,它在我的基本路由文件中
  • 这样不行。如果您在文件中使用任何变量、函数等,则需要将其导入到同一个文件中。看我的回答。这可能会有所帮助。

标签: vue.js routes vue-router


【解决方案1】:

我认为你应该先创建路由器实例,然后导出它。

export const router = new VueRouter(routes)

const routes = {
   path: '/roles',
   component: Layout2,
   ... etc.
}

然后将路由器导入到主js文件中。

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 2018-08-20
    • 2015-10-17
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2020-05-26
    • 2021-09-06
    相关资源
    最近更新 更多