router.ts 1.3 KB
Newer Older
xiaozhumaopao's avatar
xiaozhumaopao 已提交
1
/* eslint-disable @typescript-eslint/no-var-requires */
richard_1015's avatar
richard_1015 已提交
2 3
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import Index from './views/Index.vue';
A
ailululu 已提交
4
import Resource from './views/Resource.vue';
xiaozhumaopao's avatar
xiaozhumaopao 已提交
5

richard_1015's avatar
richard_1015 已提交
6
const pagesRouter: Array<RouteRecordRaw> = [];
xiaozhumaopao's avatar
xiaozhumaopao 已提交
7 8 9
const files = require.context('@/packages', true, /doc\.md$/);
files.keys().forEach(component => {
  const componentEntity = files(component).default;
richard_1015's avatar
richard_1015 已提交
10
  const name = `${component.split('/')[1]}`;
xiaozhumaopao's avatar
xiaozhumaopao 已提交
11
  pagesRouter.push({
richard_1015's avatar
richard_1015 已提交
12 13 14
    path: name,
    component: componentEntity,
    name
xiaozhumaopao's avatar
xiaozhumaopao 已提交
15 16
  });
});
richard_1015's avatar
richard_1015 已提交
17 18 19 20 21 22 23 24 25 26
const docs = require.context('@/docs', true, /\.md$/);
docs.keys().forEach(component => {
  const componentEntity = docs(component).default;
  const name = `${component.split('/')[1].replace('.md', '')}`;
  pagesRouter.push({
    path: name,
    component: componentEntity,
    name
  });
});
richard_1015's avatar
richard_1015 已提交
27 28 29
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
30
    name: '/',
xiaozhumaopao's avatar
xiaozhumaopao 已提交
31 32
    component: Index,
    children: pagesRouter
A
ailululu 已提交
33 34 35 36 37
  },
  {
    path: '/resource',
    name: '/resource',
    component: Resource
richard_1015's avatar
richard_1015 已提交
38 39
  }
];
Z
zhuzhida 已提交
40 41
const router = createRouter({
  history: createWebHashHistory(),
xiaozhumaopao's avatar
xiaozhumaopao 已提交
42
  routes,
richard_1015's avatar
richard_1015 已提交
43
  scrollBehavior(to) {
xiaozhumaopao's avatar
xiaozhumaopao 已提交
44 45 46 47 48 49 50 51
    if (to.hash) {
      const id = to.hash.split('#')[1];
      const ele = document.getElementById(id);
      setTimeout(() => {
        ele && ele.scrollIntoView(true);
      });
    }
  }
Z
zhuzhida 已提交
52 53 54
});

export default router;