router.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
  3. import Index from './views/Index.vue';
  4. import ThemeSetting from './components/ThemeSetting/Index.vue';
  5. import config from '../config/env';
  6. const pagesRouter: Array<RouteRecordRaw> = [
  7. {
  8. path: '/base',
  9. name: 'base',
  10. component: ThemeSetting
  11. }
  12. ];
  13. /** vite */
  14. const modulesPage = import.meta.glob('/src/packages/__VUE/**/doc.md');
  15. for (const path in modulesPage) {
  16. let name = (/packages\/__VUE\/(.*)\/doc.md/.exec(path) as any[])[1];
  17. pagesRouter.push({
  18. path: '/' + name,
  19. component: ThemeSetting || modulesPage[path],
  20. name
  21. });
  22. }
  23. /** vite-taro **/
  24. const modulesPageTaro = import.meta.glob('/src/packages/__VUE/**/*.taro.md');
  25. for (const path in modulesPageTaro) {
  26. let name = (/packages\/__VUE\/(.*)\/doc.taro.md/.exec(path) as any[])[1];
  27. pagesRouter.push({
  28. path: `/${name}-taro`,
  29. component: modulesPageTaro[path],
  30. name: `${name}-taro`
  31. });
  32. }
  33. const routes: Array<RouteRecordRaw> = [
  34. {
  35. path: '/',
  36. name: '/',
  37. component: Index,
  38. children: pagesRouter
  39. }
  40. ];
  41. routes.push({
  42. name: 'notFound',
  43. path: '/:path(.*)+',
  44. redirect: {
  45. name: '/'
  46. }
  47. });
  48. const router = createRouter({
  49. history: createWebHashHistory(),
  50. routes,
  51. scrollBehavior(to) {
  52. if (to.hash) {
  53. const id = to.hash.split('#')[1];
  54. const ele = document.getElementById(id);
  55. setTimeout(() => {
  56. ele && ele.scrollIntoView(true);
  57. });
  58. }
  59. }
  60. });
  61. router.afterEach((to, from) => {
  62. window.scrollTo(0, 0);
  63. });
  64. export default router;