github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/hooks/web/useTitle.ts (about)

     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  
     6  const appStore = useAppStoreWithOut()
     7  
     8  export const useTitle = (newTitle?: string) => {
     9    const { t } = useI18n()
    10    const title = ref(
    11      newTitle ? `${appStore.getTitle} - ${t(newTitle as string)}` : appStore.getTitle
    12    )
    13  
    14    watch(
    15      title,
    16      (n, o) => {
    17        if (isString(n) && n !== o && document) {
    18          document.title = n
    19        }
    20      },
    21      { immediate: true }
    22    )
    23  
    24    return title
    25  }