mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-18 10:17:16 +00:00
f26a801775
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Roe <daniel@roe.dev>
128 lines
3.0 KiB
Markdown
128 lines
3.0 KiB
Markdown
---
|
|
title: Meta Tags
|
|
description: Manage your meta tags, from Nuxt 2 to Nuxt 3.
|
|
---
|
|
|
|
Nuxt 3 provides several different ways to manage your meta tags:
|
|
1. Through your `nuxt.config`.
|
|
2. Through the [`useHead`](/docs/api/composables/use-head) [composable](/docs/getting-started/seo-meta)
|
|
3. Through [global meta components](/docs/getting-started/seo-meta)
|
|
|
|
You can customize `title`, `titleTemplate`, `base`, `script`, `noscript`, `style`, `meta`, `link`, `htmlAttrs` and `bodyAttrs`.
|
|
|
|
::callout
|
|
Nuxt currently uses [`vueuse/head`](https://github.com/vueuse/head) to manage your meta tags, but implementation details may change.
|
|
::
|
|
|
|
:read-more{to="/docs/getting-started/seo-meta"}
|
|
|
|
## Migration
|
|
|
|
1. In your `nuxt.config`, rename `head` to `meta`. Consider moving this shared meta configuration into your `app.vue` instead. (Note that objects no longer have a `hid` key for deduplication.)
|
|
2. If you need to access the component state with `head`, you should migrate to using [`useHead`](/docs/api/composables/use-head) . You might also consider using the built-in meta-components.
|
|
3. If you need to use the Options API, there is a `head()` method you can use when you use `defineNuxtComponent`.
|
|
|
|
### useHead
|
|
|
|
::code-group
|
|
|
|
```vue [Nuxt 2]
|
|
<script>
|
|
export default {
|
|
data: () => ({
|
|
title: 'My App',
|
|
description: 'My App Description'
|
|
})
|
|
head () {
|
|
return {
|
|
title: this.title,
|
|
meta: [{
|
|
hid: 'description',
|
|
name: 'description',
|
|
content: this.description
|
|
}]
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
```
|
|
|
|
```vue [Nuxt 3]
|
|
<script setup lang="ts">
|
|
const title = ref('My App')
|
|
const description = ref('My App Description')
|
|
|
|
// This will be reactive even you change title/description above
|
|
useHead({
|
|
title,
|
|
meta: [{
|
|
name: 'description',
|
|
content: description
|
|
}]
|
|
})
|
|
</script>
|
|
```
|
|
|
|
::
|
|
|
|
### Meta-components
|
|
|
|
Nuxt 3 also provides meta components that you can use to accomplish the same task. While these components look similar to HTML tags, they are provided by Nuxt and have similar functionality.
|
|
|
|
::code-group
|
|
|
|
```vue [Nuxt 2]
|
|
<script>
|
|
export default {
|
|
head () {
|
|
return {
|
|
title: 'My App',
|
|
meta: [{
|
|
hid: 'description',
|
|
name: 'description',
|
|
content: 'My App Description'
|
|
}]
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
```
|
|
|
|
```vue [Nuxt 3]
|
|
<template>
|
|
<div>
|
|
<Head>
|
|
<Title>My App</Title>
|
|
<Meta name="description" content="My app description"/>
|
|
</Head>
|
|
<!-- -->
|
|
</div>
|
|
</template>
|
|
```
|
|
|
|
::
|
|
|
|
::callout
|
|
1. Make sure you use capital letters for these component names to distinguish them from native HTML elements (`<Title>` rather than `<title>`).
|
|
2. You can place these components anywhere in your template for your page.
|
|
::
|
|
|
|
### Options API
|
|
|
|
```vue [Nuxt 3 (Options API)]
|
|
<script>
|
|
// if using options API `head` method you must use `defineNuxtComponent`
|
|
export default defineNuxtComponent({
|
|
head (nuxtApp) {
|
|
// `head` receives the nuxt app but cannot access the component instance
|
|
return {
|
|
meta: [{
|
|
name: 'description',
|
|
content: 'This is my page description.'
|
|
}]
|
|
}
|
|
}
|
|
})
|
|
</script>
|
|
```
|