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 @@