vue addRouters 刷新无效的问题

阅遍网,分享的的没一个有效的!!!!
默认前端只写死 以下路由,其他的都由后端给 数据,然后addRouters.

 {
    path: '/login',
    name: 'login',
    meta: {
      title: 'Login - 登录',
      hideInMenu: true
    },
    component: () => import('@/view/login/login.vue')
  },
  {
    path: '/',
    name: 'root',
    redirect: '/home',
    component: Main,
    meta: {
      title: '菜单根',
      hideInMenu: true,
      notCache: true
    },
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title: '主页',
          hideInMenu: true,
          notCache: true
        },
        component: () => import('@/view/single-page/home')
      }
    ]
  },

登录后,addRouters:

  let resultRoute = makeRoute(target[0].children)

          this.$router.options.routes.push(...resultRoute)
          this.$router.addRoutes(resultRoute)

但是,除了默认的/home 可以刷新外。其他的地址,刷新,都卡死了页面,无反应。

clipboard.png

即便,你在 守护者那里重新加载了缓存的数据。

 // 恢复菜单
    if (from.name === null && from.path === '/' && to.path !== '/login') {
      let resultRoute = makeRoute(store.state.app.menuAuthList[0].children)
      router.options.routes.push(...resultRoute)
      router.addRoutes(resultRoute)
    }
    
    console.info('routers now:', router.options.routes)
    if (token && to.name === LOGIN_PAGE_NAME) {
      // 已登录且要跳转的页面是登录页
      if (from.name === 'home') {
        next(false)
        iView.LoadingBar.finish()
      } else {
        next({
          name: 'home' // 跳转到home页
        })
      }
    } else {
      next()

输出 router的数据

clipboard.png

一切都有啊。我无语。求大神....

阅读 3k
1 个回答

自问自答:

// 刷新 重新加入数据: 刷新必经这里
if (store.state.user.token) {
  let resultRoute = makeRoute(store.state.app.menuAuthList[0].children)
  router.options.routes.push(...resultRoute)
  router.addRoutes(resultRoute)
}

router.beforeEach((to, from, next) => {}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题