vue-vben-admin/src/hooks/web/useTitle.ts

34 lines
862 B
TypeScript
Raw Normal View History

import { watch, unref } from 'vue';
2021-03-27 01:11:22 +08:00
import { useI18n } from '/@/hooks/web/useI18n';
import { useTitle as usePageTitle } from '@vueuse/core';
import { useGlobSetting } from '/@/hooks/setting';
import { useRouter } from 'vue-router';
2021-03-27 01:11:22 +08:00
import { REDIRECT_NAME } from '/@/router/constant';
2021-08-24 22:41:48 +08:00
/**
* Listening to page changes and dynamically changing site titles
*/
2021-03-27 01:11:22 +08:00
export function useTitle() {
const { title } = useGlobSetting();
const { t } = useI18n();
const { currentRoute } = useRouter();
2021-03-27 01:11:22 +08:00
const pageTitle = usePageTitle();
watch(
() => currentRoute.value.path,
() => {
const route = unref(currentRoute);
2021-08-24 22:41:48 +08:00
if (route.name === REDIRECT_NAME) {
return;
}
2021-03-27 01:11:22 +08:00
const tTitle = t(route?.meta?.title as string);
pageTitle.value = tTitle ? ` ${tTitle} - ${title} ` : `${title}`;
},
2021-08-24 22:41:48 +08:00
{ immediate: true },
);
2021-03-27 01:11:22 +08:00
}