router.js 980 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. ['/:pathMatch(.*)*', null, null, '/'],
  10. ];
  11. let routes = {};
  12. for (let route of myRoutes) {
  13. const [path, component, name, redirect] = route;
  14. let cleanRoute = _.pickBy({path, component, name, redirect}, _.identity);
  15. let parts = cleanRoute.path.split('~');
  16. let f = routes;
  17. for (let part of parts) {
  18. const curRoute = _.assign({}, cleanRoute, { path: part });
  19. if (!f.children)
  20. f.children = [];
  21. let r = f.children;
  22. f = _.find(r, {path: part});
  23. if (!f) {
  24. r.push(curRoute);
  25. f = curRoute;
  26. }
  27. }
  28. }
  29. routes = routes.children;
  30. export default createRouter({
  31. history: createWebHashHistory(),
  32. routes
  33. });