useTitle.ts 526 B

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