Skip to content
On this page

useTitle

Category
Last Changed
2 months ago

Reactive document title.

Demo

Title

Usage

import { useTitle } from '@vueuse/core'

const title = useTitle()
console.log(title.value) // print current title
title.value = 'Hello' // change current title
import { useTitle } from '@vueuse/core'

const title = useTitle()
console.log(title.value) // print current title
title.value = 'Hello' // change current title

Set initial title immediately

const title = useTitle('New Title')
const title = useTitle('New Title')

Pass a ref and the title will be updated when the source ref changes

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"
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"

Pass an optional template tag (Vue Meta Title Template)[https://vue-meta.nuxtjs.org/guide/metainfo.html] to update the title to be injected into this template:

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

Type Declarations

export interface UseTitleOptions extends ConfigurableDocument {
  /**
   * Observe `document.title` changes using MutationObserve
   *
   * @default false
   */
  observe?: boolean
  /**
   * The template string to parse the title (e.g., '%s | My Website')
   *
   * @default '%s'
   */
  titleTemplate?: string
}
/**
 * Reactive document title.
 *
 * @see https://vueuse.org/useTitle
 * @param newTitle
 * @param options
 */
export declare function useTitle(
  newTitle?: MaybeRef<string | null | undefined>,
  options?: UseTitleOptions
): Ref<string | null | undefined>
export declare type UseTitleReturn = ReturnType<typeof useTitle>
export interface UseTitleOptions extends ConfigurableDocument {
  /**
   * Observe `document.title` changes using MutationObserve
   *
   * @default false
   */
  observe?: boolean
  /**
   * The template string to parse the title (e.g., '%s | My Website')
   *
   * @default '%s'
   */
  titleTemplate?: string
}
/**
 * Reactive document title.
 *
 * @see https://vueuse.org/useTitle
 * @param newTitle
 * @param options
 */
export declare function useTitle(
  newTitle?: MaybeRef<string | null | undefined>,
  options?: UseTitleOptions
): Ref<string | null | undefined>
export declare type UseTitleReturn = ReturnType<typeof useTitle>

Source

SourceDemoDocs

Contributors

Anthony Fu
Alex Kozack
Antério Vieira
Michael Roberts

Changelog

v7.4.1 on 12/23/2021
357ac - feat: added optional titleTemplate injection
v4.9.2 on 5/7/2021
7ab3f - feat: new option observe to watch title changes (#483)
useTitle has loaded