vue-element-admin动态路由加载

Posted 晚星@

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue-element-admin动态路由加载相关的知识,希望对你有一定的参考价值。

store->modules->permission.js

import { asyncRoutes, constantRoutes } from '@/router'
import { getAuthMenu } from '@/api/menuModel'
import Layout from '@/layout'


/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission (roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * 后台查询的菜单数据拼装成路由格式的数据
 * @param routes
 */
export function generaMenu (routes, data) {
  data.forEach(item => {
    // alert(JSON.stringify(item))
    var menu = {}
    if (item.children.length == 0) {
      var url = item.Url.split('/')
      menu = {
        path: url[url.length - 1],
        component:
          resolve => require([`@/views${item.Url}`], resolve),
        //require.ensure("@" + item.Url + ".vue"),// loadView(item.Url), '@/views/pages/BudgetImplementation/BudgetImplementation'
        // hidden: true,
        children: [],
        name: 'menu_' + item.Key,
        meta: { title: item.Value, id: item.Key, roles: ['admin'], icon: 'lock' }
      }
      // debugger
    } else {
      menu = {
        path: item.Url,
        component: Layout,
        // hidden: true,
        children: [],
        name: 'menu_' + item.Key,
        meta: { title: item.Value, id: item.Key, roles: ['admin'], icon: 'lock' }
      }
    }
    if (item.children) {
      generaMenu(menu.children, item.children)
    }
    routes.push(menu)
  })
}
// 路由懒加载
// export const loadView = (view) => {
//   export const loadView = (view) => { return (resolve: any) => require([`@/views/${view}.vue`], resolve) }
// };

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes (routes, roles) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })
  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes ({ commit }, roles) {
    return new Promise(resolve => {
      const loadMenuData = []
      // 先查询后台并返回左侧菜单数据并把数据添加到路由
      getAuthMenu({ userName: 'admin' }).then(response => {
        let data = response

        data = response.TreeMesage
        Object.assign(loadMenuData, data)
        generaMenu(asyncRoutes, loadMenuData)
        let accessedRoutes
        if (roles.includes('admin')) {
          // alert(JSON.stringify(asyncRoutes))
          accessedRoutes = asyncRoutes || []
        } else {
          accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
        }
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)

        // generaMenu(asyncRoutes, data)
      }).catch(error => {
        console.log(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

以上是关于vue-element-admin动态路由加载的主要内容,如果未能解决你的问题,请参考以下文章

系统管理前端开发-02

vue-element-admin 登出切换用户后重新登录跳转404页面Bug-解决记录

vue-element-admin--使用体验

编程日志 Vue-element-admin

vue-element-admin 三级路由

基于 vue-element-admin 的项目总结