useTitle.ts 572 B

12345678910111213141516171819202122232425
  1. import { watch, ref } from 'vue'
  2. import { isString } from '@/utils/is'
  3. import { useAppStoreWithOut } from '@/store/modules/app'
  4. import { useI18n } from '@/hooks/web/useI18n'
  5. const appStore = useAppStoreWithOut()
  6. export const useTitle = (newTitle?: string) => {
  7. const { t } = useI18n()
  8. const title = ref(
  9. newTitle ? `${appStore.getTitle} - ${t(newTitle as string)}` : appStore.getTitle
  10. )
  11. watch(
  12. title,
  13. (n, o) => {
  14. if (isString(n) && n !== o && document) {
  15. document.title = n
  16. }
  17. },
  18. { immediate: true }
  19. )
  20. return title
  21. }