Files
vue-ts-example/src/plugins/router/index.ts
严浩 9d421c5bfe
All checks were successful
/ playwright (push) Successful in 2m19s
/ build-and-deploy-to-vercel (push) Successful in 1m9s
/ depcheck (push) Successful in 1m5s
feat: 重构插件安装方式,统一使用 install 方法并移除不必要的 setup 函数
2024-12-24 23:13:09 +08:00

34 lines
1.2 KiB
TypeScript

import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders';
import { createRouter, createWebHistory } from 'vue-router';
import { handleHotUpdate, routes } from 'vue-router/auto-routes';
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes,
strict: true,
scrollBehavior: () => ({ left: 0, top: 0 }),
});
if (import.meta.hot) handleHotUpdate(router);
if ($__DEV__) Object.assign(window, { router });
router.onError((error) => {
console.debug('🚨 [router error]: ', error);
});
export { router };
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(window, { stack: createStackGuard(router) });
}