Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useTitle.md
1---2category: Browser3---45# useTitle67Reactive document title.89::: warning10This composable isn't compatible with SSR.11:::1213## Usage1415```ts16import { useTitle } from '@vueuse/core'1718const title = useTitle()19console.log(title.value) // print current title20title.value = 'Hello' // change current title21```2223Set initial title immediately:2425```ts26import { useTitle } from '@vueuse/core'27// ---cut---28const title = useTitle('New Title')29```3031Pass a `ref` and the title will be updated when the source ref changes:3233```ts34import { useTitle } from '@vueuse/core'35import { shallowRef } from 'vue'3637const messages = shallowRef(0)3839const title = computed(() => {40return !messages.value ? 'No message' : `${messages.value} new messages`41})4243useTitle(title) // document title will match with the ref "title"44```4546Pass 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:4748```ts49import { useTitle } from '@vueuse/core'50// ---cut---51const title = useTitle('New Title', {52titleTemplate: '%s | My Awesome Website'53})54```5556::: warning57`observe` is incompatible with `titleTemplate`.58:::5960## Type Declarations6162```ts63export type UseTitleOptionsBase = {64/**65* Restore the original title when unmounted66* @param originTitle original title67* @returns restored title68*/69restoreOnUnmount?:70| false71| ((72originalTitle: string,73currentTitle: string,74) => string | null | undefined)75} & (76| {77/**78* Observe `document.title` changes using MutationObserve79* Cannot be used together with `titleTemplate` option.80*81* @default false82*/83observe?: boolean84}85| {86/**87* The template string to parse the title (e.g., '%s | My Website')88* Cannot be used together with `observe` option.89*90* @default '%s'91*/92titleTemplate?: MaybeRef<string> | ((title: string) => string)93}94)95export type UseTitleOptions = ConfigurableDocument & UseTitleOptionsBase96export type UseTitleReturn =97| ComputedRef<string | null | undefined>98| Ref<string | null | undefined>99/**100* Reactive document title.101*102* @see https://vueuse.org/useTitle103* @param newTitle104* @param options105* @description It's not SSR compatible. Your value will be applied only on client-side.106*/107export declare function useTitle(108newTitle: ReadonlyRefOrGetter<string | null | undefined>,109options?: UseTitleOptions,110): ComputedRef<string | null | undefined>111export declare function useTitle(112newTitle?: MaybeRef<string | null | undefined>,113options?: UseTitleOptions,114): Ref<string | null | undefined>115```116