fix: 处理页面title闪烁问题

This commit is contained in:
ZhuRui
2026-07-27 10:06:45 +08:00
parent 7b6fd59023
commit 4c7a0709a4
+11 -4
View File
@@ -1,6 +1,13 @@
import router from '@/router'; import router from '@/router';
import { auth } from '@/hooks/useAuth'; import { auth } from '@/hooks/useAuth';
const appTitle = import.meta.env.VITE_APP_TITLE || 'CPMS 运营平台';
/** 导航完成后设置页面标题,避免动态路由加载前误匹配 404 导致 title 闪烁 */
function updateDocumentTitle(title?: string) {
document.title = title ? `${title} - ${appTitle}` : appTitle;
}
/** /**
* 全局路由守卫 * 全局路由守卫
* 核心流程: * 核心流程:
@@ -15,10 +22,6 @@ import { auth } from '@/hooks/useAuth';
* matched 信息带过去,导致仍然匹配到 /:pathMatch(.*)*。 * matched 信息带过去,导致仍然匹配到 /:pathMatch(.*)*。
*/ */
router.beforeEach(async (to, _from, next) => { router.beforeEach(async (to, _from, next) => {
// 设置页面标题
const appTitle = import.meta.env.VITE_APP_TITLE || 'CPMS 运营平台';
document.title = to.meta.title ? `${to.meta.title} - ${appTitle}` : appTitle;
// ── /login 特殊处理 ── // ── /login 特殊处理 ──
if (to.path === '/login') { if (to.path === '/login') {
if (auth.isLoggedIn()) { if (auth.isLoggedIn()) {
@@ -51,3 +54,7 @@ router.beforeEach(async (to, _from, next) => {
next(); next();
}); });
router.afterEach((to) => {
updateDocumentTitle(to.meta.title as string | undefined);
});