Files
vue-ts-example/src/plugins/router.ts
严浩 31909c906d
All checks were successful
/ depcheck (push) Successful in 2m3s
/ build-and-deploy-to-vercel (push) Successful in 2m52s
/ lint-build-and-check (push) Successful in 2m57s
/ surge (push) Successful in 2m32s
/ playwright (push) Successful in 3m10s
feat: 添加 eslint-plugin-unicorn 依赖,更新 ESLint 配置以支持新规则
2025-03-14 12:50:54 +08:00

59 lines
1.8 KiB
TypeScript

import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders';
import { setupLayouts } from 'virtual:meta-layouts';
// import { createGetRoutes, setupLayouts } from 'virtual:generated-layouts';
import { createRouter, createWebHistory } from 'vue-router/auto';
import { handleHotUpdate, routes } from 'vue-router/auto-routes';
const setupLayoutsResult = setupLayouts(routes);
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: /* routes ?? */ setupLayoutsResult,
scrollBehavior: (_to, _from, savedPosition) => {
return savedPosition ?? { left: 0, top: 0 };
},
strict: true,
});
if (import.meta.hot) handleHotUpdate(router);
if ($__DEV__) Object.assign(globalThis, { router });
router.onError((error) => {
console.debug('🚨 [router error]:', error);
});
export { router, setupLayoutsResult };
export function install({ app }: { app: import('vue').App<Element> }) {
app
// Register the plugin before the router
.use(DataLoaderPlugin, { router })
// adding the router will trigger the initial navigation
.use(router);
}
// ========================================================================
// =========================== Router Guards ==============================
// ========================================================================
{
// 警告:路由守卫的创建顺序会影响执行流程,请勿调整
createNProgressGuard(router);
createLogGuard(router);
Object.assign(globalThis, { stack: createStackGuard(router) });
}
/*
definePage({
meta: { },
});
*/
declare module 'vue-router' {
interface RouteMeta {
/**
* @description 是否在菜单中隐藏
*/
hidden?: boolean;
/**
* @description 菜单标题
*/
title?: string;
}
}
export { createGetRoutes } from 'virtual:meta-layouts';