router.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import _ from 'lodash';
  4. import App from './components/App.vue';
  5. import CardIndex from './components/CardIndex/CardIndex.vue';
  6. import Search from './components/CardIndex/Search/Search.vue';
  7. import Reader from './components/Reader/Reader.vue';
  8. //import Forum from './components/Forum/Forum.vue';
  9. import Income from './components/Income/Income.vue';
  10. import Sources from './components/Sources/Sources.vue';
  11. import Settings from './components/Settings/Settings.vue';
  12. import Help from './components/Help/Help.vue';
  13. import NotFound404 from './components/NotFound404/NotFound404.vue';
  14. const myRoutes = [
  15. ['/', null, null, '/cardindex'],
  16. ['/cardindex', CardIndex, null, '/cardindex/search' ],
  17. ['/cardindex~search', Search ],
  18. //['/cardindex~card/:authorId', CardIndex ],
  19. ['/reader', Reader ],
  20. ['/income', Income ],
  21. ['/sources', Sources ],
  22. ['/settings', Settings ],
  23. ['/help', Help ],
  24. ['*', NotFound404 ],
  25. ];
  26. let routes = {};
  27. for (let route of myRoutes) {
  28. const [path, component, name, redirect] = route;
  29. let cleanRoute = _.pickBy({path, component, name, redirect}, _.identity);
  30. let parts = cleanRoute.path.split('~');
  31. let f = routes;
  32. for (let part of parts) {
  33. const curRoute = _.assign({}, cleanRoute, { path: part });
  34. if (!f.children)
  35. f.children = [];
  36. let r = f.children;
  37. f = _.find(r, {path: part});
  38. if (!f) {
  39. r.push(curRoute);
  40. f = curRoute;
  41. }
  42. }
  43. }
  44. routes = routes.children;
  45. Vue.use(VueRouter);
  46. export default new VueRouter({
  47. routes
  48. });