router.js 1007 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { createRouter, createWebHashHistory } from 'vue-router';
  2. import _ from 'lodash';
  3. const Search = () => import('./components/Search/Search.vue');
  4. const myRoutes = [
  5. ['/', Search],
  6. ['/author', Search],
  7. ['/series', Search],
  8. ['/title', Search],
  9. ['/extended', Search],
  10. ['/:pathMatch(.*)*', null, null, '/'],
  11. ];
  12. let routes = {};
  13. for (let route of myRoutes) {
  14. const [path, component, name, redirect] = route;
  15. let cleanRoute = _.pickBy({path, component, name, redirect}, _.identity);
  16. let parts = cleanRoute.path.split('~');
  17. let f = routes;
  18. for (let part of parts) {
  19. const curRoute = _.assign({}, cleanRoute, { path: part });
  20. if (!f.children)
  21. f.children = [];
  22. let r = f.children;
  23. f = _.find(r, {path: part});
  24. if (!f) {
  25. r.push(curRoute);
  26. f = curRoute;
  27. }
  28. }
  29. }
  30. routes = routes.children;
  31. export default createRouter({
  32. history: createWebHashHistory(),
  33. routes
  34. });