当前位置:   article > 正文

运营后台动态路由(element ui)_elementui 路由name

elementui 路由name

路由文件

 

 

store中配置文件

  1. import { asyncRoutes, constantRoutes } from '@/router';
  2. import { getRouterList } from '@/api/user';
  3. import Layout from '@/layout';
  4. const _import = file => require(`@/views/${file}/index.vue`).default;
  5. /**
  6. * Filter asynchronous routing tables by recursion
  7. * @param routes asyncRoutes
  8. * @param roles
  9. */
  10. export function generaMenu(routes, data) {
  11. data.forEach(item => {
  12. // console.log(item.componentPath);
  13. const menu = {
  14. path: item.routerPath,
  15. hidden: !item.available,
  16. children: [],
  17. meta: { icon: item.icon, title: item.resourceName }
  18. };
  19. // if (item.componentPath) {
  20. // 判断 item.componentPath 是否等于 'Layout',若是则直接替换成引入的 Layout 组件
  21. if (item.componentPath === 'Layout') {
  22. menu.component = Layout;
  23. menu.meta = { icon: item.icon, title: item.resourceName };
  24. } else {
  25. // item.componentPath 不等于 'Layout',则说明它是组件路径地址,因此直接替换成路由引入的方法
  26. try {
  27. menu.component = _import(item.componentPath);
  28. menu.name = item.routerPath.indexOf('/') === 0 ? item.routerPath.substring(0) : item.routerPath;
  29. menu.meta = item.routerPath.indexOf('/') === 0 ? null : { icon: item.icon, title: item.resourceName };
  30. // 此处用reqiure比较好,import引入变量会有各种莫名的错误
  31. // menu.component = (() => import(`@/views/${item.componentPath}`));
  32. } catch (e) {
  33. console.log(e);
  34. menu.component = null;
  35. }
  36. }
  37. // }
  38. if (item.children && item.children.length > 0) {
  39. generaMenu(menu.children, item.children);
  40. }
  41. if (item.children.length > 1) {
  42. menu.redirect = `${item.routerPath}/${item.children[0].routerPath}`;
  43. }
  44. // console.log('组件位置', menu.path);
  45. routes.push(menu);
  46. });
  47. // console.log(routes);
  48. return routes;
  49. }
  50. const state = {
  51. routes: [],
  52. addRoutes: []
  53. };
  54. const mutations = {
  55. SET_ROUTES: (state, routes) => {
  56. state.addRoutes = routes;
  57. state.routes = constantRoutes.concat(routes);
  58. }
  59. };
  60. const actions = {
  61. generateRoutes({ commit }, roles) {
  62. return new Promise(resolve => {
  63. // 先查询后台并返回左侧菜单数据并把数据添加到路由
  64. getRouterList().then(res => {
  65. const menuData = Object.assign([], res.body);
  66. const tempAsyncRoutes = Object.assign([], asyncRoutes);
  67. const accessedRoutes = generaMenu(tempAsyncRoutes, menuData);
  68. // console.log('路由', accessedRoutes);
  69. commit('SET_ROUTES', accessedRoutes);
  70. resolve(accessedRoutes);
  71. });
  72. });
  73. }
  74. };
  75. export default {
  76. namespaced: true,
  77. state,
  78. mutations,
  79. actions
  80. };

 

路由拦截中

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

闽ICP备14008679号