vueuse 中文文档

Vue useTitle 修改网页标题

Vue 中你可以通过 useTitle 来修改网页的标题,当与 Nuxt3 一起使用时,这个函数将不会自动导入, 如果你想使用 VueUse 中的函数,请使用显式导入。

代码示例

import { useTitle } from '@vueuse/core'

const title = useTitle()
console.log(title.value) // print current title
title.value = '当前网页标题 www.itxst.com' // 修改网页标题
//或者这样使用
const title = useTitle('New Title')

也可以这样使用

import { useTitle } from '@vueuse/core'

const messages = ref(0)

const title = computed(() => {
  return !messages.value ? 'No message' : `${messages.value} new messages`
})

useTitle(title) // document title will match with the ref "title"

通过Vue Meta Title Template 配合使用

const title = useTitle('New Title', { titleTemplate: '%s | My Awesome Website' })

在线例子

在线试一试

类型定义

export type UseTitleOptionsBase = {
  /**
   * Restore the original title when unmounted
   * @param originTitle original title
   * @returns restored title
   */
  restoreOnUnmount?:
    | false
    | ((
        originalTitle: string,
        currentTitle: string,
      ) => string | null | undefined)
} & (
  | {
      /**
       * Observe `document.title` changes using MutationObserve
       * Cannot be used together with `titleTemplate` option.
       *
       * @default false
       */
      observe?: boolean
    }
  | {
      /**
       * The template string to parse the title (e.g., '%s | My Website')
       * Cannot be used together with `observe` option.
       *
       * @default '%s'
       */
      titleTemplate?: MaybeRef<string> | ((title: string) => string)
    }
)
export type UseTitleOptions = ConfigurableDocument & UseTitleOptionsBase
export declare function useTitle(
  newTitle: ReadonlyRefOrGetter<string | null | undefined>,
  options?: UseTitleOptions,
): ComputedRef<string | null | undefined>
export declare function useTitle(
  newTitle?: MaybeRef<string | null | undefined>,
  options?: UseTitleOptions,
): Ref<string | null | undefined>
export type UseTitleReturn = ReturnType<typeof useTitle>
Catalog
vueuse 入门 State 状态 Elements 元素 Browser 浏览器 useBluetooth 蓝牙函数 useBreakpoints 网页网格断点 useBroadcastChannel 跨网页通讯 useBrowserLocation 响应式location useClipboard 粘贴复制 useColorMode 皮肤颜色切换 useCssVar 修改css变量 useEventListener 事件监听 useEyeDropper 取色器 useFavicon 设置浏览器窗口图标 useFileDialog 文件选择器 useFileSystemAccess 操作本地文件 useFullscreen 全屏模式 useGamepad 游戏手柄 useImage 图片 useMediaControls 视频音频控制 useMediaQuery 媒体查询 useMemory 响应式内存信息 useObjectUrl 响应式对象URL usePerformanceObserver 性能指标 usePermission 判断 api 权限 usePreferredColorScheme 获取系统主题 usePreferredContrast 色彩对比度 usePreferredDark 黑暗主题 usePreferredLanguages 用户系统语言 useScreenOrientation 屏幕方向API useScreenSafeArea 屏幕安全区域 useScriptTag 动态加载js文件 useShare 分享功能 useStyleTag 动态注入CSS useTextareaAutosize textarea自适应高度 useUrlSearchParams 获取设置URL参数 useTextDirection 文字方向 useTitle 修改网页标题 useVibrate 手机振动功能 useWakeLock 防止屏幕变黑或锁屏 useWebNotification 显示桌面通知 useWebWorker 创建多线程任务 useWebWorkerFn 多线程函数 Sensors 感应监测