router.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { createRouter, createWebHashHistory } from 'vue-router';
  2. import _ from 'lodash';
  3. const CardIndex = () => import('./components/CardIndex/CardIndex.vue');
  4. const Search = () => import('./components/CardIndex/Search/Search.vue');
  5. const Card = () => import('./components/CardIndex/Card/Card.vue');
  6. const Book = () => import('./components/CardIndex/Book/Book.vue');
  7. const History = () => import('./components/CardIndex/History/History.vue');
  8. //немедленная загрузка
  9. //import Reader from './components/Reader/Reader.vue';
  10. const Reader = () => import('./components/Reader/Reader.vue');
  11. const ExternalLibs = () => import('./components/ExternalLibs/ExternalLibs.vue');
  12. const Income = () => import('./components/Income/Income.vue');
  13. const Sources = () => import('./components/Sources/Sources.vue');
  14. const Settings = () => import('./components/Settings/Settings.vue');
  15. const Help = () => import('./components/Help/Help.vue');
  16. const NotFound404 = () => import('./components/NotFound404/NotFound404.vue');
  17. const myRoutes = [
  18. ['/', null, null, '/cardindex'],
  19. ['/cardindex', CardIndex],
  20. ['/cardindex~search', Search],
  21. ['/cardindex~card', Card],
  22. ['/cardindex~card/:authorId', Card],
  23. ['/cardindex~book', Book],
  24. ['/cardindex~book/:bookId', Book],
  25. ['/cardindex~history', History],
  26. ['/reader', Reader],
  27. ['/external-libs', ExternalLibs],
  28. ['/income', Income],
  29. ['/sources', Sources],
  30. ['/settings', Settings],
  31. ['/help', Help],
  32. ['/404', NotFound404],
  33. ['/(.*)', null, null, '/cardindex'],
  34. ];
  35. let routes = {};
  36. for (let route of myRoutes) {
  37. const [path, component, name, redirect] = route;
  38. let cleanRoute = _.pickBy({path, component, name, redirect}, _.identity);
  39. let parts = cleanRoute.path.split('~');
  40. let f = routes;
  41. for (let part of parts) {
  42. const curRoute = _.assign({}, cleanRoute, { path: part });
  43. if (!f.children)
  44. f.children = [];
  45. let r = f.children;
  46. f = _.find(r, {path: part});
  47. if (!f) {
  48. r.push(curRoute);
  49. f = curRoute;
  50. }
  51. }
  52. }
  53. routes = routes.children;
  54. export default createRouter({
  55. history: createWebHashHistory(),
  56. routes
  57. });