router.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import _ from 'lodash';
  4. import App from './components/App.vue';
  5. const CardIndex = () => import('./components/CardIndex/CardIndex.vue');
  6. const Search = () => import('./components/CardIndex/Search/Search.vue');
  7. const Card = () => import('./components/CardIndex/Card/Card.vue');
  8. const Book = () => import('./components/CardIndex/Book/Book.vue');
  9. const History = () => import('./components/CardIndex/History/History.vue');
  10. const Reader = () => import('./components/Reader/Reader.vue');
  11. //const Forum = () => import('./components/Forum/Forum.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. ['/income', Income ],
  28. ['/sources', Sources ],
  29. ['/settings', Settings ],
  30. ['/help', Help ],
  31. ['*', null, null, '/cardindex' ],
  32. ];
  33. let routes = {};
  34. for (let route of myRoutes) {
  35. const [path, component, name, redirect] = route;
  36. let cleanRoute = _.pickBy({path, component, name, redirect}, _.identity);
  37. let parts = cleanRoute.path.split('~');
  38. let f = routes;
  39. for (let part of parts) {
  40. const curRoute = _.assign({}, cleanRoute, { path: part });
  41. if (!f.children)
  42. f.children = [];
  43. let r = f.children;
  44. f = _.find(r, {path: part});
  45. if (!f) {
  46. r.push(curRoute);
  47. f = curRoute;
  48. }
  49. }
  50. }
  51. routes = routes.children;
  52. Vue.use(VueRouter);
  53. export default new VueRouter({
  54. routes
  55. });