useTitle.ts 990 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { watch, unref } 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 } from 'vue-router';
  6. import { useLocaleStore } from '/@/store/modules/locale';
  7. import { REDIRECT_NAME } from '/@/router/constant';
  8. /**
  9. * Listening to page changes and dynamically changing site titles
  10. */
  11. export function useTitle() {
  12. const { title } = useGlobSetting();
  13. const { t } = useI18n();
  14. const { currentRoute } = useRouter();
  15. const localeStore = useLocaleStore();
  16. const pageTitle = usePageTitle();
  17. watch(
  18. [() => currentRoute.value.path, () => localeStore.getLocale],
  19. () => {
  20. const route = unref(currentRoute);
  21. if (route.name === REDIRECT_NAME) {
  22. return;
  23. }
  24. const tTitle = t(route?.meta?.title as string);
  25. pageTitle.value = tTitle ? ` ${tTitle} - ${title} ` : `${title}`;
  26. },
  27. { immediate: true }
  28. );
  29. }