From 4c7a0709a47da5d240ecea2ba39e5af2508e8afa Mon Sep 17 00:00:00 2001 From: ZhuRui Date: Mon, 27 Jul 2026 10:06:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=84=E7=90=86=E9=A1=B5=E9=9D=A2titl?= =?UTF-8?q?e=E9=97=AA=E7=83=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/guard.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/router/guard.ts b/src/router/guard.ts index 20893c9..b2beeae 100644 --- a/src/router/guard.ts +++ b/src/router/guard.ts @@ -1,6 +1,13 @@ import router from '@/router'; 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(.*)*。 */ 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 特殊处理 ── if (to.path === '/login') { if (auth.isLoggedIn()) { @@ -51,3 +54,7 @@ router.beforeEach(async (to, _from, next) => { next(); }); + +router.afterEach((to) => { + updateDocumentTitle(to.meta.title as string | undefined); +});