当前位置:   article > 正文

(2)vue-element-admin使用:动态路由_vue-element-admin动态路由

vue-element-admin动态路由

        vue-element-admin的登录退出,可以看上一篇。后端代码不做讲解,会说明需要返回的数据格式。

  本篇章参考:https://ke.qq.com/course/3323814?taid=10963075825055654  

         想要源码的可以联系视频里的老师或者加我qq814216044。

        主要修改文件为src/store/modules/permission.js把其中的generateRoutes方法改为如下所示

  1. const actions = {
  2. generateRoutes: async function({ commit }, roles) {
  3. // 从后台请求所有的路由信息
  4. const res = await getRoutes()
  5. // 定义一个变量,用来存放可以访问的路由表
  6. const dbAsyncRoutes = res.data
  7. // 过滤掉空的children和把component字符改编为对象
  8. const myAsyncRoutes = dbAsyncRoutes.filter(curr => {
  9. if (curr.children == null || curr.children.length === 0) {
  10. delete curr.children
  11. }
  12. return replaceComponent(curr)
  13. })
  14. let accessedRoutes
  15. // 判断当前的角色列表中,是否有包含admin
  16. if (roles.includes('admin')) {
  17. // 所有路由都可以被访问,将ansyncRoutes改造成从数据库中获取
  18. accessedRoutes = myAsyncRoutes || []
  19. } else {
  20. // 根据角色,过滤掉不能访问的路由表
  21. accessedRoutes = filterAsyncRoutes(myAsyncRoutes, roles)
  22. }
  23. // commit
  24. commit('SET_ROUTES', accessedRoutes)
  25. // 成功返回
  26. // resolve(accessedRoutes)
  27. return accessedRoutes
  28. }
  29. }

         由于方法中replaceComponent方法是新增,方法如下所部,并附上修改有import需要导入的文件。

  1. import { constantRoutes, componentMap } from '@/router'
  2. import { getRoutes } from '@/api/auth'
  3. // 替换route对象中的component
  4. function replaceComponent(comp) {
  5. if (comp.component && typeof (comp.component) === 'string') {
  6. comp.component = componentMap[comp.component]
  7. }
  8. if (comp.children && comp.children.length > 0) {
  9. for (let i = 0; i < comp.children.length; i++) {
  10. comp.children[i] = replaceComponent(comp.children[i])
  11. }
  12. }
  13. return comp
  14. }

        到此src/store/modules/permission.js已经修改完,由于后端代码已经是确定的,也就是说登录后会返回一个我们已经写好的路由页面,所以我们要复制已经写好的页面过来。如下图所示

        

         没有的文件就复制过去,已经有的文件就把所有内容复制粘贴。在上面的方法中有一个componentMap值,他是在router/index.vue中配置的如下所示

 源代码:

  1. /**
  2. * 定义组件名称和组件对象的map对象
  3. */
  4. export const componentMap = {
  5. 'layout': require('@/layout').default,
  6. 'redirect_index': () => import('@/views/redirect/index').then(m => m.default),
  7. 'login_index': () => import('@/views/login/index').then(m => m.default),
  8. 'login_auth_redirect': () => import('@/views/login/auth-redirect').then(m => m.default),
  9. 'error_page_404': () => import('@/views/error-page/404').then(m => m.default),
  10. 'error_page_401': () => import('@/views/error-page/401').then(m => m.default),
  11. 'dashboard_index': () => import('@/views/dashboard/index').then(m => m.default),
  12. 'documentation_index': () => import('@/views/documentation/index').then(m => m.default),
  13. 'guide_index': () => import('@/views/guide/index').then(m => m.default),
  14. 'profile_index': () => import('@/views/profile/index').then(m => m.default),
  15. 'permission_menu': () => import('@/views/permission/menu').then(m => m.default),
  16. 'permission_resource': () => import('@/views/permission/permResource').then(m => m.default),
  17. 'permission_role': () => import('@/views/permission/role').then(m => m.default),
  18. 'user_role': () => import('@/views/permission/user').then(m => m.default),
  19. 'icons_index': () => import('@/views/icons/index').then(m => m.default),
  20. 'clipboard_index': () => import('@/views/clipboard/index').then(m => m.default)
  21. }

         此时动态路由配置成功,登录后显示如下

 

         红色框中为自己添加的页面,如果代码中已经写好了页面,想在添加到动态路由中可以根据我下面的操作来,我以框架自带的页面进行配置,下面这个为一级菜单

         二级菜单如下

         代码中需要在router/index添加一段代码,每增加一个页面都要有添加,指明组件在哪。

     可以根据框架原配的路由进行参考,如下所示

        得到路由返回的数据如下所示        到此,动态路由前端添加完成。我会在第三篇权限管理中讲解该框架登录、注册、动态路由和权限管理的过程和参考视频的思路。

        (如有不对的地方可以留言私信指出谢谢)

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58942
推荐阅读
相关标签
  

闽ICP备14008679号