vue-element-admin icon indicating copy to clipboard operation
vue-element-admin copied to clipboard

router.addRoutes(accessRoutes)不生效

Open Lencamo opened this issue 2 years ago • 5 comments

动态路由实现问题

1、代码位置

  • @/permission.js
router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()

  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
    } else {
      // determine whether the user has obtained his permission roles through getInfo
      const hasRoles = store.getters.roles && store.getters.roles.length > 0
      if (hasRoles) {
        next()
      } else {
        try {
          // get user info
          // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
          const { roles } = await store.dispatch('user/getInfo')

          // generate accessible routes map based on roles
          const accessRoutes = await store.dispatch('permission/generateRoutes', roles)

          // dynamically add accessible routes
          router.addRoutes(accessRoutes) 👀👀🚩👀👀这里👀👀🚩👀👀

          // hack method to ensure that addRoutes is complete
          // set the replace: true, so the navigation will not leave a history record
          next({ ...to, replace: true })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

2、分析

          const accessRoutes = await store.dispatch('permission/generateRoutes', role)
          // console.log(accessRoutes)

          router.options.routes.push(...accessRoutes)  // 新增代码 🚩解决问题
          router.addRoutes(accessRoutes)

          // console.log(router.options.routes)

3、提问

请问这是什么原因造成的呢?🤔

Lencamo avatar Nov 07 '22 14:11 Lencamo

我也碰到这个问题

zhouzy-creator avatar Nov 14 '22 01:11 zhouzy-creator

我也遇到了这个问题,如果不加router.options.routes.push(...accessRoutes),侧边导航栏会不显示路由,但是可以手动输入地址去访问页面

aoesun avatar Mar 16 '23 07:03 aoesun

我也遇到了这个问题,使用你的方法得以解决,蹲一个原因解答

weizihao30 avatar Apr 26 '23 13:04 weizihao30

帮大忙了楼主,卡了一天调这个

lsiuf avatar Aug 22 '23 06:08 lsiuf

动态路由实现问题

1、代码位置

  • @/permission.js
router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()

  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
    } else {
      // determine whether the user has obtained his permission roles through getInfo
      const hasRoles = store.getters.roles && store.getters.roles.length > 0
      if (hasRoles) {
        next()
      } else {
        try {
          // get user info
          // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
          const { roles } = await store.dispatch('user/getInfo')

          // generate accessible routes map based on roles
          const accessRoutes = await store.dispatch('permission/generateRoutes', roles)

          // dynamically add accessible routes
          router.addRoutes(accessRoutes) 👀👀🚩👀👀这里👀👀🚩👀👀

          // hack method to ensure that addRoutes is complete
          // set the replace: true, so the navigation will not leave a history record
          next({ ...to, replace: true })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

2、分析

          const accessRoutes = await store.dispatch('permission/generateRoutes', role)
          // console.log(accessRoutes)

          router.options.routes.push(...accessRoutes)  // 新增代码 🚩解决问题
          router.addRoutes(accessRoutes)

          // console.log(router.options.routes)

3、提问

请问这是什么原因造成的呢?🤔

参考同一个问题 https://github.com/PanJiaChen/vue-element-admin/issues/2370 里的回答,修改layout/components/Sidebar/index.vue ,遍历路由生成菜单的时候要使用permission_routes

Sherry00124 avatar Jan 12 '24 09:01 Sherry00124