dynamicMuen.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import store from '../store/store.js';
  2. const staticMuen = [
  3. {
  4. path: '/msg',
  5. component: () => import(/* webpackChunkName: "messages" */ '@views/messages'),
  6. meta: { title: '消息中心' }
  7. },
  8. {
  9. path: '/403',
  10. component: () => import(/* webpackChunkName: "404" */ '@views/errorPage/403.vue'),
  11. meta: { title: '403' }
  12. },
  13. {
  14. path: '/404',
  15. component: () => import(/* webpackChunkName: "404" */ '@views/errorPage/404.vue'),
  16. meta: { title: '404' }
  17. },
  18. {
  19. path: '*',
  20. redirect: '/404'
  21. }
  22. ];
  23. //路由格式
  24. // 自定义路由文件
  25. const setListMunt = (arrList, item) => {
  26. let newOb = {};
  27. newOb = {
  28. path: `/${item.linkPath}`,
  29. component: () => import(`@/views/${item.linkPath}.vue`),
  30. meta: { title: item.name }
  31. };
  32. arrList.push(newOb);
  33. };
  34. const dimension = (arrList, arr) => {
  35. arr.forEach((item, index) => {
  36. if (!!item.children) {
  37. dimension(arrList, item.children);
  38. } else {
  39. setListMunt(arrList, item);
  40. }
  41. });
  42. };
  43. //添加路由
  44. const AddRt = (router, dynamicMuenList) => {
  45. let NEWdynamicMuenList = [...dynamicMuenList, ...staticMuen];
  46. if (NEWdynamicMuenList.length !== 0) {
  47. NEWdynamicMuenList.forEach((item) => {
  48. router.options.routes[0].children.push(item);
  49. });
  50. router.addRoutes(router.options.routes);
  51. }
  52. };
  53. export const funcRouter = (arr, data, router) => {
  54. dimension(arr, data);
  55. store.commit('setMenuList', data);
  56. AddRt(router, arr);
  57. };