useTitle.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { watch, unref, ref } from 'vue';
  2. import { useI18n } from '/@/hooks/web/useI18n';
  3. import { useTitle as usePageTitle } from '@vueuse/core';
  4. import { useGlobSetting } from '/@/hooks/setting';
  5. import { useRouter, Route } from 'vue-router';
  6. import { useLocaleStore } from '/@/store/modules/locale';
  7. import { REDIRECT_NAME } from '/@/router/constant';
  8. import { Route } from 'ant-design-vue/lib/breadcrumb/Breadcrumb';
  9. /**
  10. * Listening to page changes and dynamically changing site titles
  11. */
  12. export function useTitle() {
  13. const { title } = useGlobSetting();
  14. const { t } = useI18n();
  15. const { currentRoute } = useRouter();
  16. const localeStore = useLocaleStore();
  17. const pageTitle = usePageTitle();
  18. const currentRouteObj = ref<Route>(unref(currentRoute));
  19. watch(
  20. [() => currentRouteObj.path, () => localeStore.getLocale],
  21. () => {
  22. const route = unref(currentRoute);
  23. if (route.name === REDIRECT_NAME) {
  24. return;
  25. }
  26. const tTitle = t(route?.meta?.title as string);
  27. pageTitle.value = tTitle ? ` ${tTitle} - ${title} ` : `${title}`;
  28. },
  29. { immediate: true }
  30. );
  31. }