dbsd_kczx/src/hooks/web/useTitle.ts

36 lines
991 B
TypeScript
Raw Normal View History

2021-10-20 14:32:09 +08:00
import { watch, unref } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useTitle as usePageTitle } from '@vueuse/core';
import { useGlobSetting } from '/@/hooks/setting';
import { useRouter } from 'vue-router';
2022-03-10 09:47:29 +08:00
import { useLocaleStore } from '/@/store/modules/locale';
2021-10-20 14:32:09 +08:00
import { REDIRECT_NAME } from '/@/router/constant';
2022-03-10 09:47:29 +08:00
/**
* Listening to page changes and dynamically changing site titles
*/
2021-10-20 14:32:09 +08:00
export function useTitle() {
const { title } = useGlobSetting();
const { t } = useI18n();
const { currentRoute } = useRouter();
2022-03-10 09:47:29 +08:00
const localeStore = useLocaleStore();
2021-10-20 14:32:09 +08:00
const pageTitle = usePageTitle();
watch(
2022-03-10 09:47:29 +08:00
[() => currentRoute.value.path, () => localeStore.getLocale],
2021-10-20 14:32:09 +08:00
() => {
const route = unref(currentRoute);
2022-03-10 09:47:29 +08:00
2021-10-20 14:32:09 +08:00
if (route.name === REDIRECT_NAME) {
return;
}
const tTitle = t(route?.meta?.title as string);
pageTitle.value = tTitle ? ` ${tTitle} - ${title} ` : `${title}`;
},
2022-03-10 09:47:29 +08:00
{ immediate: true },
2021-10-20 14:32:09 +08:00
);
}