router.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { createRouter, createWebHashHistory } from 'vue-router';
  2. import _ from 'lodash';
  3. //немедленная загрузка
  4. //import Reader from './components/Reader/Reader.vue';
  5. const Reader = () => import('./components/Reader/Reader.vue');
  6. const ExternalLibs = () => import('./components/ExternalLibs/ExternalLibs.vue');
  7. const myRoutes = [
  8. ['/', null, null, '/reader'],
  9. ['/reader', Reader],
  10. ['/external-libs', ExternalLibs],
  11. ['/:pathMatch(.*)*', null, null, '/reader'],
  12. ];
  13. let routes = {};
  14. for (let route of myRoutes) {
  15. const [path, component, name, redirect] = route;
  16. let cleanRoute = _.pickBy({path, component, name, redirect}, _.identity);
  17. let parts = cleanRoute.path.split('~');
  18. let f = routes;
  19. for (let part of parts) {
  20. const curRoute = _.assign({}, cleanRoute, { path: part });
  21. if (!f.children)
  22. f.children = [];
  23. let r = f.children;
  24. f = _.find(r, {path: part});
  25. if (!f) {
  26. r.push(curRoute);
  27. f = curRoute;
  28. }
  29. }
  30. }
  31. routes = routes.children;
  32. export default createRouter({
  33. history: createWebHashHistory(),
  34. routes
  35. });