-
-
Notifications
You must be signed in to change notification settings - Fork 984
docs: add documentation for titleTemplate
#5057
Changes from all commits
4663a2f
5170d33
4121af7
b616cdc
071c14e
b746828
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,9 +13,7 @@ For example: | |||||||||||||||||||||||
| ```vue | ||||||||||||||||||||||||
| <script setup> | ||||||||||||||||||||||||
| useHead({ | ||||||||||||||||||||||||
| titleTemplate: 'My App - %s', | ||||||||||||||||||||||||
| // or, instead: | ||||||||||||||||||||||||
| // titleTemplate: (title) => `My App - ${title}`, | ||||||||||||||||||||||||
| title: 'My App', | ||||||||||||||||||||||||
| viewport: 'width=device-width, initial-scale=1, maximum-scale=1', | ||||||||||||||||||||||||
| charset: 'utf-8', | ||||||||||||||||||||||||
| meta: [ | ||||||||||||||||||||||||
|
|
@@ -31,6 +29,34 @@ useHead({ | |||||||||||||||||||||||
| ::ReadMore{link="/api/composables/use-head"} | ||||||||||||||||||||||||
| :: | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ## Title Templates | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| You can use the `titleTemplate` option to provide a dynamic template for customizing the title of your site, for example, by adding the name of your site to the title of every page. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Create a `title.js` plugin within your `plugins` folder, with the following code: | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||
| export default defineNuxtPlugin(() => { | ||||||||||||||||||||||||
| useHead({ | ||||||||||||||||||||||||
| titleTemplate: (titleChunk) => { | ||||||||||||||||||||||||
| return titleChunk ? `${titleChunk} - Site Title` : 'Site Title'; | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Then you can set a title as you usually would: | ||||||||||||||||||||||||
|
Comment on lines
+38
to
+48
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ```vue [/pages/my-page.vue] | ||||||||||||||||||||||||
| <script setup> | ||||||||||||||||||||||||
| useHead({ | ||||||||||||||||||||||||
| title: 'My Page' | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we needed a plugin for it to work? As the current function approach does nothing.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the function approach should work everywhere but in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even with the following example in <script setup>
useHead({
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - Site` : 'Site: Default Text';
},
});
</script>
<template>
<div>
<NuxtLayout>
<NuxtPage/>
</NuxtLayout>
</div>
</template>If you don't pass a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's a separate issue.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR to fix it: #5064 |
||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| </script> | ||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| This will now appear as 'My Page - Site Title' in the browser tab. You can also pass `null` to default to the site title. | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ## Meta Components | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Nuxt provides `<Title>`, `<Base>`, `<Script>`, `<Style>`, `<Meta>`, `<Link>`, `<Body>`, `<Html>` and `<Head>` components so that you can interact directly with your metadata within your component's template. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.