tdesign-vue-next-starter/src/router/index.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-28 11:14:13 +08:00
import { useRoute, createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import baseRouters from './modules/base';
import componentsRouters from './modules/components';
import othersRouters from './modules/others';
// 关于单层路由meta 中设置 { single: true } 即可为单层路由,{ hidden: true } 即可在侧边栏隐藏该路由
// 存放动态路由
export const asyncRouterList: Array<RouteRecordRaw> = [...baseRouters, ...componentsRouters, ...othersRouters];
// 存放固定的路由
const defaultRouterList: Array<RouteRecordRaw> = [
{
path: '/login',
name: 'login',
component: () => import('@/pages/login/index.vue'),
},
{
path: '/',
redirect: '/dashboard/base',
component: () => import('@/layouts/blank.vue'),
},
2022-03-07 00:08:57 +08:00
{
path: '/:w+',
name: '404Page',
redirect: '/result/404',
},
];
2022-03-07 00:08:57 +08:00
export const allRoutes = [...defaultRouterList, ...asyncRouterList];
2022-04-28 11:14:13 +08:00
export const getActive = (maxLevel = 2): string => {
const route = useRoute();
if (!route.path) {
return '';
}
return route.path
.split('/')
.filter((_item: string, index: number) => index <= maxLevel && index > 0)
.map((item: string) => `/${item}`)
.join('');
};
const router = createRouter({
2021-12-23 10:45:56 +08:00
history: createWebHashHistory(),
2022-03-07 00:08:57 +08:00
routes: allRoutes,
scrollBehavior() {
return {
el: '#app',
top: 0,
behavior: 'smooth',
};
},
});
export default router;