router.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Resource from './views/Resource.vue';
  5. import Main from './views/Main.vue';
  6. import { HttpClient } from '../service/HttpClient';
  7. const pagesRouter: Array<RouteRecordRaw> = [];
  8. const files = require.context('@/packages', true, /doc\.md$/);
  9. files.keys().forEach(component => {
  10. const componentEntity = files(component).default;
  11. const name = `${component.split('/')[1]}`;
  12. pagesRouter.push({
  13. path: '/' + name,
  14. component: componentEntity,
  15. name
  16. });
  17. });
  18. const docs = require.context('@/docs', true, /\.md$/);
  19. docs.keys().forEach(component => {
  20. const componentEntity = docs(component).default;
  21. const name = `${component.split('/')[1].replace('.md', '')}`;
  22. pagesRouter.push({
  23. path: '/' + name,
  24. component: componentEntity,
  25. name
  26. });
  27. });
  28. const routes: Array<RouteRecordRaw> = [
  29. { path: '/', redirect: '/main' },
  30. {
  31. path: '/main',
  32. name: 'main',
  33. component: Main
  34. // children: pagesRouter
  35. },
  36. {
  37. path: '/index',
  38. name: 'index',
  39. component: Index,
  40. children: pagesRouter
  41. },
  42. {
  43. path: '/resource',
  44. name: 'resource',
  45. component: Resource
  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. new HttpClient().request('/user/saveVisitInfo', 'post', {
  63. headers: '',
  64. componentName: to.path.split('/')[1]
  65. });
  66. });
  67. export default router;