home-page/src/router/index.ts

69 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createRouter, createWebHistory } from 'vue-router'
import NotFoundView from '@/views/NotFoundView.vue'
import { getStaticRoutes } from './routes'
function getRoutes() {
return [
...getStaticRoutes().map(route => {
return {
...route, component: {
'/': () => import('@/views/HomeView.vue'),
'/tools': () => import('@/views/ToolsView.vue'),
'/blogs': () => import('@/views/BlogsView.vue'),
'/tools/image-converter': () => import('@/views/tools/ImageConverterView.vue'),
'/tools/qing-audio': () => import('@/views/tools/QingAudioConverterView.vue'),
'/tools/desktop-engine': () => import('@/views/tools/DesktopEngineView.vue'),
}[route.path] || (() => import('@/views/NotFoundView.vue')),
meta: route.path === '/tools/desktop-engine' ? { hideLayout: true } : {},
}
}),
/* ...Object.values(mdFiles).map(module => {
const { frontmatter, default: defaultExport } = module;
const { short, long } = generatePath(frontmatter);
return {
path: long,
name: frontmatter.title,
component: defaultExport,
alias: short
};
}) */
];
}
const router = createRouter({
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { top: 0 }
}
},
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/404',
name: '未找到页面',
component: NotFoundView
},
// 所有未定义路由全部重定向到404页
{
path: '/:pathMatch(.*)*',
redirect: '/404'
},
{
path: '/blogs/:blogPath',
component: () => import('@/views/blogs/IndexView.vue'),
},
...getRoutes()
],
})
router.beforeEach((to, from, next) => {
if (to.name?.toString())
document.title = `${to.name?.toString() || ''} - 鹅.net`
next()
})
export default router