Back to Vueuse

useTitle

packages/core/useTitle/index.md

14.3.01.1 KB
Original Source

useTitle

Reactive document title.

::: warning This composable isn't compatible with SSR. :::

Usage

ts
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:

ts
import { useTitle } from '@vueuse/core'
// ---cut---
const title = useTitle('New Title')

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

ts
import { useTitle } from '@vueuse/core'
import { shallowRef } from 'vue'

const messages = shallowRef(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 to update the title to be injected into this template:

ts
import { useTitle } from '@vueuse/core'
// ---cut---
const title = useTitle('New Title', {
  titleTemplate: '%s | My Awesome Website'
})

::: warning observe is incompatible with titleTemplate. :::