vue-vben-admin/apps/antd-view/src/router/guard/index.ts

56 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-06-01 23:15:29 +08:00
import { preferences } from '@vben-core/preferences';
2024-05-19 21:20:42 +08:00
import type { Router } from 'vue-router';
2024-05-22 22:03:41 +08:00
import { $t } from '@vben/locales';
import { startProgress, stopProgress } from '@vben/utils';
2024-05-22 22:03:41 +08:00
import { useTitle } from '@vueuse/core';
2024-05-19 21:20:42 +08:00
import { configAccessGuard } from './access';
/**
*
* @param router
*/
function configCommonGuard(router: Router) {
2024-06-01 22:17:52 +08:00
// 记录已经加载的页面
2024-05-19 21:20:42 +08:00
const loadedPaths = new Set<string>();
router.beforeEach(async (to) => {
2024-06-01 22:17:52 +08:00
// 页面加载进度条
2024-06-01 23:15:29 +08:00
if (preferences.transition.progress) {
2024-05-19 21:20:42 +08:00
startProgress();
}
to.meta.loaded = loadedPaths.has(to.path);
return true;
});
router.afterEach((to) => {
// 记录页面是否加载,如果已经加载,后续的页面切换动画等效果不在重复执行
loadedPaths.add(to.path);
2024-06-01 22:17:52 +08:00
// 关闭页面加载进度条
2024-06-01 23:15:29 +08:00
if (preferences.transition.progress) {
2024-05-19 21:20:42 +08:00
stopProgress();
}
2024-05-22 22:03:41 +08:00
// 动态修改标题
2024-06-01 23:15:29 +08:00
if (preferences.app.dynamicTitle) {
2024-05-22 22:03:41 +08:00
const { title } = to.meta;
2024-06-01 23:15:29 +08:00
useTitle(`${$t(title)} - ${preferences.app.name}`);
2024-05-22 22:03:41 +08:00
}
2024-05-19 21:20:42 +08:00
});
}
/**
*
* @param router
*/
function createRouteGuard(router: Router) {
/** 通用 */
configCommonGuard(router);
/** 权限访问 */
configAccessGuard(router);
}
export { createRouteGuard };