router.js 906 B

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