Skip to content

Commit d769005

Browse files
author
sheepzh
committed
Fix document's title
1 parent f0dc410 commit d769005

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/app/layout/menu.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ function renderMenu(menu: _MenuGroup, props: UnwrapRef<_RouteProps>) {
174174
return h(ElMenuItemGroup, { title }, () => menu.children.map(item => renderMenuLeaf(item, props)))
175175
}
176176

177+
async function initTitle(allMenus: _MenuGroup[], router: Router) {
178+
await router.isReady()
179+
const currentPath = router.currentRoute.value.path
180+
for (const group of allMenus) {
181+
for (const { route, title } of group.children) {
182+
const docTitle = route === currentPath && t(msg => msg.menu[title])
183+
if (docTitle) {
184+
document.title = docTitle
185+
return
186+
}
187+
}
188+
}
189+
}
190+
177191
const _default = defineComponent({
178192
name: "LayoutMenu",
179193
setup() {
@@ -182,11 +196,12 @@ const _default = defineComponent({
182196
current: useRoute()
183197
})
184198

185-
onMounted(() => document.title = t(msg => msg.menu.dashboard))
199+
const allMenus = generateMenus()
200+
onMounted(() => initTitle(allMenus, useRouter()))
186201

187202
return () => h(ElMenu,
188203
{ defaultActive: routeProps.current.path },
189-
() => generateMenus().map(menu => renderMenu(menu, routeProps))
204+
() => allMenus.map(menu => renderMenu(menu, routeProps))
190205
)
191206
}
192207
})

0 commit comments

Comments
 (0)