From 51972dd3e43730c6612310971ca96cf2ac8d461e Mon Sep 17 00:00:00 2001 From: Benny Guo Date: Thu, 10 Aug 2023 06:08:40 +0800 Subject: [PATCH] fix: dynamic page title with i18n menu config --- src/hooks/usePageTitle.ts | 37 +++++++++++++++++++++++++++++++++++++ src/views/About.vue | 20 ++++++++++++++------ src/views/Archives.vue | 29 +++++++++++++++++++---------- src/views/Category.vue | 21 ++++++--------------- src/views/Links.vue | 26 ++++++++++++++++++++------ src/views/Tag.vue | 10 +++++++--- 6 files changed, 103 insertions(+), 40 deletions(-) create mode 100644 src/hooks/usePageTitle.ts diff --git a/src/hooks/usePageTitle.ts b/src/hooks/usePageTitle.ts new file mode 100644 index 00000000..f77e903f --- /dev/null +++ b/src/hooks/usePageTitle.ts @@ -0,0 +1,37 @@ +import { useAppStore } from '@/stores/app' +import { useMetaStore } from '@/stores/meta' +import { ref, watch } from 'vue' +import { useRoute } from 'vue-router' + +export default function usePageTitle() { + const pageTitle = ref() + const appStore = useAppStore() + const metaStore = useMetaStore() + const route = useRoute() + + const updateTitle = (locale?: string | undefined) => { + const currentLocale = (locale ?? appStore.locale) === 'cn' ? 'cn' : 'en' + const menuName = String(route.name) + const routeInfo = + appStore.themeConfig.menu.menus[ + menuName.charAt(0).toUpperCase() + menuName.slice(1) + ] + pageTitle.value = + (routeInfo.i18n && routeInfo.i18n[currentLocale]) || routeInfo.name + metaStore.setTitle(pageTitle.value) + } + + watch( + () => appStore.locale, + value => { + if (value) { + updateTitle(value) + } + } + ) + + return { + pageTitle, + updateTitle + } +} diff --git a/src/views/About.vue b/src/views/About.vue index 886619bb..71dbe23c 100644 --- a/src/views/About.vue +++ b/src/views/About.vue @@ -1,17 +1,19 @@ diff --git a/src/views/Archives.vue b/src/views/Archives.vue index 362ef601..d313b7cd 100644 --- a/src/views/Archives.vue +++ b/src/views/Archives.vue @@ -1,8 +1,8 @@