@nuxt/docs 4.2.0 → 4.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/1.getting-started/01.introduction.md +1 -1
- package/1.getting-started/02.installation.md +3 -3
- package/1.getting-started/03.configuration.md +27 -27
- package/1.getting-started/04.views.md +5 -5
- package/1.getting-started/05.assets.md +8 -8
- package/1.getting-started/06.styling.md +15 -15
- package/1.getting-started/07.routing.md +10 -6
- package/1.getting-started/08.seo-meta.md +3 -3
- package/1.getting-started/09.transitions.md +10 -10
- package/1.getting-started/10.data-fetching.md +16 -16
- package/1.getting-started/11.state-management.md +3 -3
- package/1.getting-started/12.error-handling.md +6 -6
- package/1.getting-started/13.server.md +6 -6
- package/1.getting-started/14.layers.md +32 -13
- package/1.getting-started/16.deployment.md +1 -1
- package/1.getting-started/17.testing.md +35 -4
- package/1.getting-started/18.upgrade.md +42 -34
- package/{2.guide/1.directory-structure → 2.directory-structure}/0.nuxt.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/0.output.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.assets.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.components.md +6 -6
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.composables.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.layouts.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.middleware.md +5 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.pages.md +17 -17
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.plugins.md +3 -7
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/1.utils.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/3.app.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/3.error.md +1 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.content.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.modules.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.node_modules.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.public.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.server.md +7 -7
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.shared.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.env.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtignore.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtrc.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.nuxt-config.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.package.md +1 -1
- package/2.directory-structure/3.tsconfig.md +69 -0
- package/2.directory-structure/index.md +61 -0
- package/{2.guide → 3.guide}/0.index.md +4 -7
- package/{2.guide/2.concepts/3.rendering.md → 3.guide/1.concepts/1.rendering.md} +4 -30
- package/{2.guide/2.concepts/2.vuejs-development.md → 3.guide/1.concepts/10.vuejs-development.md} +7 -6
- package/{2.guide/2.concepts/10.nuxt-lifecycle.md → 3.guide/1.concepts/2.nuxt-lifecycle.md} +32 -25
- package/{2.guide/2.concepts/1.auto-imports.md → 3.guide/1.concepts/3.auto-imports.md} +7 -7
- package/{2.guide/2.concepts → 3.guide/1.concepts}/4.server-engine.md +3 -3
- package/{2.guide/2.concepts → 3.guide/1.concepts}/5.modules.md +1 -1
- package/{2.guide/2.concepts → 3.guide/1.concepts}/7.esm.md +3 -2
- package/{2.guide/2.concepts → 3.guide/1.concepts}/8.typescript.md +15 -38
- package/{2.guide/2.concepts → 3.guide/1.concepts}/9.code-style.md +1 -1
- package/{2.guide/5.best-practices → 3.guide/2.best-practices}/hydration.md +1 -1
- package/{2.guide/5.best-practices → 3.guide/2.best-practices}/performance.md +2 -2
- package/3.guide/3.ai/.navigation.yml +3 -0
- package/3.guide/3.ai/1.mcp.md +255 -0
- package/3.guide/3.ai/2.llms-txt.md +65 -0
- package/{2.guide → 3.guide}/4.recipes/1.custom-routing.md +5 -5
- package/{2.guide → 3.guide}/4.recipes/2.vite-plugin.md +1 -1
- package/{2.guide → 3.guide}/4.recipes/3.custom-usefetch.md +1 -1
- package/{2.guide → 3.guide}/4.recipes/4.sessions-and-authentication.md +1 -1
- package/{2.guide/3.going-further → 3.guide/5.going-further}/1.events.md +2 -3
- package/{2.guide/3.going-further → 3.guide/5.going-further}/1.experimental-features.md +10 -10
- package/{2.guide/3.going-further → 3.guide/5.going-further}/1.features.md +1 -1
- package/{2.guide/3.going-further → 3.guide/5.going-further}/1.internals.md +3 -2
- package/{2.guide/3.going-further → 3.guide/5.going-further}/10.runtime-config.md +2 -2
- package/{2.guide/3.going-further → 3.guide/5.going-further}/2.hooks.md +2 -2
- package/{2.guide/3.going-further → 3.guide/5.going-further}/3.modules.md +78 -11
- package/{2.guide/3.going-further → 3.guide/5.going-further}/6.nuxt-app.md +5 -5
- package/{2.guide/3.going-further → 3.guide/5.going-further}/7.layers.md +42 -25
- package/{2.guide/3.going-further → 3.guide/5.going-further}/9.debugging.md +1 -1
- package/{3.api → 4.api}/1.components/10.nuxt-picture.md +1 -1
- package/{3.api → 4.api}/1.components/11.teleports.md +1 -1
- package/{3.api → 4.api}/1.components/12.nuxt-route-announcer.md +1 -3
- package/{3.api → 4.api}/1.components/13.nuxt-time.md +0 -2
- package/{3.api → 4.api}/1.components/2.nuxt-page.md +3 -3
- package/{3.api → 4.api}/1.components/3.nuxt-layout.md +5 -5
- package/{3.api → 4.api}/1.components/4.nuxt-link.md +11 -11
- package/{3.api → 4.api}/1.components/5.nuxt-loading-indicator.md +1 -1
- package/{3.api → 4.api}/1.components/6.nuxt-error-boundary.md +1 -1
- package/{3.api → 4.api}/1.components/7.nuxt-welcome.md +2 -2
- package/{3.api → 4.api}/2.composables/use-app-config.md +1 -1
- package/{3.api → 4.api}/2.composables/use-async-data.md +76 -13
- package/4.api/2.composables/use-cookie.md +183 -0
- package/{3.api → 4.api}/2.composables/use-fetch.md +33 -33
- package/{3.api → 4.api}/2.composables/use-head-safe.md +37 -20
- package/4.api/2.composables/use-head.md +169 -0
- package/{3.api → 4.api}/2.composables/use-hydration.md +24 -18
- package/4.api/2.composables/use-lazy-async-data.md +96 -0
- package/4.api/2.composables/use-lazy-fetch.md +111 -0
- package/{3.api → 4.api}/2.composables/use-nuxt-app.md +7 -7
- package/{3.api → 4.api}/2.composables/use-nuxt-data.md +1 -1
- package/{3.api → 4.api}/2.composables/use-request-fetch.md +1 -1
- package/{3.api → 4.api}/2.composables/use-response-header.md +1 -1
- package/{3.api → 4.api}/2.composables/use-route-announcer.md +0 -2
- package/{3.api → 4.api}/2.composables/use-route.md +2 -2
- package/4.api/2.composables/use-router.md +94 -0
- package/{3.api → 4.api}/2.composables/use-runtime-config.md +1 -1
- package/{3.api → 4.api}/2.composables/use-runtime-hook.md +1 -1
- package/{3.api → 4.api}/2.composables/use-state.md +1 -1
- package/{3.api → 4.api}/3.utils/$fetch.md +1 -1
- package/{3.api → 4.api}/3.utils/abort-navigation.md +3 -3
- package/{3.api → 4.api}/3.utils/add-route-middleware.md +1 -1
- package/{3.api → 4.api}/3.utils/call-once.md +0 -2
- package/{3.api → 4.api}/3.utils/define-lazy-hydration-component.md +4 -4
- package/{3.api → 4.api}/3.utils/define-nuxt-component.md +1 -1
- package/4.api/3.utils/define-nuxt-plugin.md +102 -0
- package/{3.api → 4.api}/3.utils/define-nuxt-route-middleware.md +2 -2
- package/{3.api → 4.api}/3.utils/define-page-meta.md +14 -14
- package/{3.api → 4.api}/3.utils/navigate-to.md +15 -15
- package/{3.api → 4.api}/3.utils/on-before-route-leave.md +1 -1
- package/{3.api → 4.api}/3.utils/on-before-route-update.md +1 -1
- package/{3.api → 4.api}/3.utils/refresh-cookie.md +1 -3
- package/{3.api → 4.api}/3.utils/update-app-config.md +2 -2
- package/{3.api → 4.api}/4.commands/add.md +10 -10
- package/4.api/4.commands/analyze.md +42 -0
- package/4.api/4.commands/build-module.md +42 -0
- package/4.api/4.commands/build.md +47 -0
- package/{3.api → 4.api}/4.commands/cleanup.md +6 -6
- package/4.api/4.commands/dev.md +60 -0
- package/{3.api → 4.api}/4.commands/devtools.md +7 -7
- package/4.api/4.commands/generate.md +42 -0
- package/4.api/4.commands/info.md +33 -0
- package/4.api/4.commands/init.md +50 -0
- package/4.api/4.commands/module.md +84 -0
- package/4.api/4.commands/prepare.md +41 -0
- package/4.api/4.commands/preview.md +44 -0
- package/4.api/4.commands/test.md +40 -0
- package/4.api/4.commands/typecheck.md +44 -0
- package/4.api/4.commands/upgrade.md +37 -0
- package/{3.api → 4.api}/5.kit/1.modules.md +18 -18
- package/{3.api → 4.api}/5.kit/10.templates.md +23 -23
- package/{3.api → 4.api}/5.kit/11.nitro.md +35 -35
- package/{3.api → 4.api}/5.kit/14.builder.md +21 -21
- package/{3.api → 4.api}/5.kit/16.layers.md +12 -12
- package/{3.api → 4.api}/5.kit/2.programmatic.md +2 -2
- package/{3.api → 4.api}/5.kit/4.autoimports.md +18 -18
- package/4.api/5.kit/5.components.md +146 -0
- package/4.api/6.advanced/1.hooks.md +105 -0
- package/{3.api → 4.api}/6.nuxt-config.md +29 -28
- package/5.community/3.reporting-bugs.md +1 -1
- package/5.community/4.contribution.md +1 -1
- package/5.community/5.framework-contribution.md +8 -8
- package/5.community/6.roadmap.md +25 -25
- package/5.community/7.changelog.md +10 -0
- package/6.bridge/1.overview.md +1 -1
- package/6.bridge/2.typescript.md +1 -1
- package/6.bridge/3.bridge-composition-api.md +1 -1
- package/6.bridge/4.plugins-and-middleware.md +2 -2
- package/7.migration/11.server.md +1 -1
- package/7.migration/2.configuration.md +3 -3
- package/7.migration/20.module-authors.md +1 -1
- package/7.migration/3.auto-imports.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +2 -2
- package/7.migration/6.pages-and-layouts.md +6 -6
- package/README.md +1 -1
- package/package.json +1 -1
- package/2.guide/1.directory-structure/3.tsconfig.md +0 -38
- package/3.api/2.composables/use-cookie.md +0 -183
- package/3.api/2.composables/use-head.md +0 -69
- package/3.api/2.composables/use-lazy-async-data.md +0 -47
- package/3.api/2.composables/use-lazy-fetch.md +0 -55
- package/3.api/2.composables/use-router.md +0 -94
- package/3.api/3.utils/define-nuxt-plugin.md +0 -102
- package/3.api/4.commands/analyze.md +0 -42
- package/3.api/4.commands/build-module.md +0 -42
- package/3.api/4.commands/build.md +0 -47
- package/3.api/4.commands/dev.md +0 -60
- package/3.api/4.commands/generate.md +0 -42
- package/3.api/4.commands/info.md +0 -33
- package/3.api/4.commands/init.md +0 -50
- package/3.api/4.commands/module.md +0 -84
- package/3.api/4.commands/prepare.md +0 -41
- package/3.api/4.commands/preview.md +0 -44
- package/3.api/4.commands/test.md +0 -40
- package/3.api/4.commands/typecheck.md +0 -44
- package/3.api/4.commands/upgrade.md +0 -37
- package/3.api/5.kit/5.components.md +0 -146
- package/3.api/6.advanced/1.hooks.md +0 -105
- /package/{2.guide/1.directory-structure → 2.directory-structure}/.navigation.yml +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/.navigation.yml +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/1.app/3.app-config.md +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/2.gitignore.md +0 -0
- /package/{2.guide → 3.guide}/.navigation.yml +0 -0
- /package/{2.guide/2.concepts → 3.guide/1.concepts}/.navigation.yml +0 -0
- /package/{2.guide/5.best-practices → 3.guide/2.best-practices}/.navigation.yml +0 -0
- /package/{2.guide/5.best-practices → 3.guide/2.best-practices}/plugins.md +0 -0
- /package/{2.guide → 3.guide}/4.recipes/.navigation.yml +0 -0
- /package/{2.guide/3.going-further → 3.guide/5.going-further}/.navigation.yml +0 -0
- /package/{2.guide/3.going-further → 3.guide/5.going-further}/11.nightly-release-channel.md +0 -0
- /package/{2.guide/3.going-further → 3.guide/5.going-further}/4.kit.md +0 -0
- /package/{2.guide/3.going-further → 3.guide/5.going-further}/index.md +0 -0
- /package/{3.api → 4.api}/.navigation.yml +0 -0
- /package/{3.api → 4.api}/1.components/.navigation.yml +0 -0
- /package/{3.api → 4.api}/1.components/1.client-only.md +0 -0
- /package/{3.api → 4.api}/1.components/1.dev-only.md +0 -0
- /package/{3.api → 4.api}/1.components/1.nuxt-client-fallback.md +0 -0
- /package/{3.api → 4.api}/1.components/8.nuxt-island.md +0 -0
- /package/{3.api → 4.api}/1.components/9.nuxt-img.md +0 -0
- /package/{3.api → 4.api}/2.composables/.navigation.yml +0 -0
- /package/{3.api → 4.api}/2.composables/on-prehydrate.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-error.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-loading-indicator.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-preview-mode.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-event.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-header.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-headers.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-url.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-seo-meta.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-server-seo-meta.md +0 -0
- /package/{3.api → 4.api}/3.utils/.navigation.yml +0 -0
- /package/{3.api → 4.api}/3.utils/clear-error.md +0 -0
- /package/{3.api → 4.api}/3.utils/clear-nuxt-data.md +0 -0
- /package/{3.api → 4.api}/3.utils/clear-nuxt-state.md +0 -0
- /package/{3.api → 4.api}/3.utils/create-error.md +0 -0
- /package/{3.api → 4.api}/3.utils/define-route-rules.md +0 -0
- /package/{3.api → 4.api}/3.utils/on-nuxt-ready.md +0 -0
- /package/{3.api → 4.api}/3.utils/prefetch-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/preload-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/preload-route-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/prerender-routes.md +0 -0
- /package/{3.api → 4.api}/3.utils/refresh-nuxt-data.md +0 -0
- /package/{3.api → 4.api}/3.utils/reload-nuxt-app.md +0 -0
- /package/{3.api → 4.api}/3.utils/set-page-layout.md +0 -0
- /package/{3.api → 4.api}/3.utils/set-response-status.md +0 -0
- /package/{3.api → 4.api}/3.utils/show-error.md +0 -0
- /package/{3.api → 4.api}/4.commands/.navigation.yml +0 -0
- /package/{3.api → 4.api}/5.kit/.navigation.yml +0 -0
- /package/{3.api → 4.api}/5.kit/10.runtime-config.md +0 -0
- /package/{3.api → 4.api}/5.kit/12.resolving.md +0 -0
- /package/{3.api → 4.api}/5.kit/13.logging.md +0 -0
- /package/{3.api → 4.api}/5.kit/15.examples.md +0 -0
- /package/{3.api → 4.api}/5.kit/3.compatibility.md +0 -0
- /package/{3.api → 4.api}/5.kit/6.context.md +0 -0
- /package/{3.api → 4.api}/5.kit/7.pages.md +0 -0
- /package/{3.api → 4.api}/5.kit/8.layout.md +0 -0
- /package/{3.api → 4.api}/5.kit/9.head.md +0 -0
- /package/{3.api → 4.api}/5.kit/9.plugins.md +0 -0
- /package/{3.api → 4.api}/6.advanced/.navigation.yml +0 -0
- /package/{3.api → 4.api}/6.advanced/2.import-meta.md +0 -0
- /package/{3.api → 4.api}/index.md +0 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: useHead
|
|
3
|
+
description: useHead customizes the head properties of individual pages of your Nuxt app.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/unjs/unhead/blob/main/packages/vue/src/composables.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
The `useHead` composable allows you to manage your head tags in a programmatic and reactive way, powered by [Unhead](https://unhead.unjs.io). It lets you customize the meta tags, links, scripts, and other elements in the `<head>` section of your HTML document.
|
|
14
|
+
|
|
15
|
+
```vue [app/app.vue]
|
|
16
|
+
<script setup lang="ts">
|
|
17
|
+
useHead({
|
|
18
|
+
title: 'My App',
|
|
19
|
+
meta: [
|
|
20
|
+
{ name: 'description', content: 'My amazing site.' },
|
|
21
|
+
],
|
|
22
|
+
bodyAttrs: {
|
|
23
|
+
class: 'test',
|
|
24
|
+
},
|
|
25
|
+
script: [{ innerHTML: 'console.log(\'Hello world\')' }],
|
|
26
|
+
})
|
|
27
|
+
</script>
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
::warning
|
|
31
|
+
If the data comes from a user or other untrusted source, we recommend you check out [`useHeadSafe`](/docs/4.x/api/composables/use-head-safe).
|
|
32
|
+
::
|
|
33
|
+
|
|
34
|
+
::note
|
|
35
|
+
The properties of `useHead` can be dynamic, accepting `ref`, `computed` and `reactive` properties. The `meta` parameter can also accept a function returning an object to make the entire object reactive.
|
|
36
|
+
::
|
|
37
|
+
|
|
38
|
+
## Type
|
|
39
|
+
|
|
40
|
+
```ts [Signature]
|
|
41
|
+
export function useHead (meta: MaybeComputedRef<MetaObject>): void
|
|
42
|
+
|
|
43
|
+
interface MetaObject {
|
|
44
|
+
title?: string
|
|
45
|
+
titleTemplate?: string | ((title?: string) => string)
|
|
46
|
+
base?: Base
|
|
47
|
+
link?: Link[]
|
|
48
|
+
meta?: Meta[]
|
|
49
|
+
style?: Style[]
|
|
50
|
+
script?: Script[]
|
|
51
|
+
noscript?: Noscript[]
|
|
52
|
+
htmlAttrs?: HtmlAttributes
|
|
53
|
+
bodyAttrs?: BodyAttributes
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
See [@unhead/schema](https://github.com/unjs/unhead/blob/main/packages/vue/src/types/schema.ts) for more detailed types.
|
|
58
|
+
|
|
59
|
+
## Parameters
|
|
60
|
+
|
|
61
|
+
`meta`: An object accepting head metadata properties to customize the page's `<head>` section. All properties support reactive values (`ref`, `computed`, `reactive`) or can be a function returning the metadata object.
|
|
62
|
+
|
|
63
|
+
| Property | Type | Description |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `title` | `string` | Sets the page title. |
|
|
66
|
+
| `titleTemplate` | `string \| ((title?: string) => string)` | Configures a dynamic template to customize the page title. Can be a string with `%s` placeholder or a function. |
|
|
67
|
+
| `base` | `Base` | Sets the `<base>` tag for the document. |
|
|
68
|
+
| `link` | `Link[]` | Array of link objects. Each element is mapped to a `<link>` tag, where object properties correspond to HTML attributes. |
|
|
69
|
+
| `meta` | `Meta[]` | Array of meta objects. Each element is mapped to a `<meta>` tag, where object properties correspond to HTML attributes. |
|
|
70
|
+
| `style` | `Style[]` | Array of style objects. Each element is mapped to a `<style>` tag, where object properties correspond to HTML attributes. |
|
|
71
|
+
| `script` | `Script[]` | Array of script objects. Each element is mapped to a `<script>` tag, where object properties correspond to HTML attributes. |
|
|
72
|
+
| `noscript` | `Noscript[]` | Array of noscript objects. Each element is mapped to a `<noscript>` tag, where object properties correspond to HTML attributes. |
|
|
73
|
+
| `htmlAttrs` | `HtmlAttributes` | Sets attributes of the `<html>` tag. Each object property is mapped to the corresponding attribute. |
|
|
74
|
+
| `bodyAttrs` | `BodyAttributes` | Sets attributes of the `<body>` tag. Each object property is mapped to the corresponding attribute. |
|
|
75
|
+
|
|
76
|
+
## Return Values
|
|
77
|
+
|
|
78
|
+
This composable does not return any value. It registers the head metadata with Unhead, which manages the actual DOM updates.
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
### Basic Meta Tags
|
|
83
|
+
|
|
84
|
+
```vue [app/pages/about.vue]
|
|
85
|
+
<script setup lang="ts">
|
|
86
|
+
useHead({
|
|
87
|
+
title: 'About Us',
|
|
88
|
+
meta: [
|
|
89
|
+
{ name: 'description', content: 'Learn more about our company' },
|
|
90
|
+
{ property: 'og:title', content: 'About Us' },
|
|
91
|
+
{ property: 'og:description', content: 'Learn more about our company' },
|
|
92
|
+
],
|
|
93
|
+
})
|
|
94
|
+
</script>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Reactive Meta Tags
|
|
98
|
+
|
|
99
|
+
```vue [app/pages/profile.vue]
|
|
100
|
+
<script setup lang="ts">
|
|
101
|
+
const profile = ref({ name: 'John Doe' })
|
|
102
|
+
|
|
103
|
+
useHead({
|
|
104
|
+
title: computed(() => profile.value.name),
|
|
105
|
+
meta: [
|
|
106
|
+
{
|
|
107
|
+
name: 'description',
|
|
108
|
+
content: computed(() => `Profile page for ${profile.value.name}`),
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
})
|
|
112
|
+
</script>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Using a Function for Full Reactivity
|
|
116
|
+
|
|
117
|
+
```vue [app/pages/dynamic.vue]
|
|
118
|
+
<script setup lang="ts">
|
|
119
|
+
const count = ref(0)
|
|
120
|
+
|
|
121
|
+
useHead(() => ({
|
|
122
|
+
title: `Count: ${count.value}`,
|
|
123
|
+
meta: [
|
|
124
|
+
{ name: 'description', content: `Current count is ${count.value}` },
|
|
125
|
+
],
|
|
126
|
+
}))
|
|
127
|
+
</script>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Adding External Scripts and Styles
|
|
131
|
+
|
|
132
|
+
```vue [app/pages/external.vue]
|
|
133
|
+
<script setup lang="ts">
|
|
134
|
+
useHead({
|
|
135
|
+
link: [
|
|
136
|
+
{
|
|
137
|
+
rel: 'stylesheet',
|
|
138
|
+
href: 'https://cdn.example.com/styles.css',
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
script: [
|
|
142
|
+
{
|
|
143
|
+
src: 'https://cdn.example.com/script.js',
|
|
144
|
+
async: true,
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
})
|
|
148
|
+
</script>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Body and HTML Attributes
|
|
152
|
+
|
|
153
|
+
```vue [app/pages/themed.vue]
|
|
154
|
+
<script setup lang="ts">
|
|
155
|
+
const isDark = ref(true)
|
|
156
|
+
|
|
157
|
+
useHead({
|
|
158
|
+
htmlAttrs: {
|
|
159
|
+
lang: 'en',
|
|
160
|
+
class: computed(() => isDark.value ? 'dark' : 'light'),
|
|
161
|
+
},
|
|
162
|
+
bodyAttrs: {
|
|
163
|
+
class: 'themed-page',
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
</script>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
:read-more{to="/docs/4.x/getting-started/seo-meta"}
|
|
@@ -8,6 +8,8 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
+
`useHydration` is a built-in composable that provides a way to set data on the server side every time a new HTTP request is made and receive that data on the client side. This way `useHydration` allows you to take full control of the hydration cycle.
|
|
12
|
+
|
|
11
13
|
::note
|
|
12
14
|
This is an advanced composable, primarily designed for use within plugins, mostly used by Nuxt modules.
|
|
13
15
|
::
|
|
@@ -16,14 +18,24 @@ This is an advanced composable, primarily designed for use within plugins, mostl
|
|
|
16
18
|
`useHydration` is designed to **ensure state synchronization and restoration during SSR**. If you need to create a globally reactive state that is SSR-friendly in Nuxt, [`useState`](/docs/4.x/api/composables/use-state) is the recommended choice.
|
|
17
19
|
::
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
## Usage
|
|
20
22
|
|
|
21
23
|
The data returned from the `get` function on the server is stored in `nuxtApp.payload` under the unique key provided as the first parameter to `useHydration`. During hydration, this data is then retrieved on the client, preventing redundant computations or API calls.
|
|
22
24
|
|
|
23
|
-
## Usage
|
|
24
|
-
|
|
25
25
|
::code-group
|
|
26
26
|
|
|
27
|
+
```ts [With useHydration]
|
|
28
|
+
export default defineNuxtPlugin((nuxtApp) => {
|
|
29
|
+
const myStore = new MyStore()
|
|
30
|
+
|
|
31
|
+
useHydration(
|
|
32
|
+
'myStoreState',
|
|
33
|
+
() => myStore.getState(),
|
|
34
|
+
data => myStore.setState(data),
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
27
39
|
```ts [Without useHydration]
|
|
28
40
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
29
41
|
const myStore = new MyStore()
|
|
@@ -41,18 +53,6 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
41
53
|
}
|
|
42
54
|
})
|
|
43
55
|
```
|
|
44
|
-
|
|
45
|
-
```ts [With useHydration]
|
|
46
|
-
export default defineNuxtPlugin((nuxtApp) => {
|
|
47
|
-
const myStore = new MyStore()
|
|
48
|
-
|
|
49
|
-
useHydration(
|
|
50
|
-
'myStoreState',
|
|
51
|
-
() => myStore.getState(),
|
|
52
|
-
data => myStore.setState(data),
|
|
53
|
-
)
|
|
54
|
-
})
|
|
55
|
-
```
|
|
56
56
|
::
|
|
57
57
|
|
|
58
58
|
## Type
|
|
@@ -63,6 +63,12 @@ export function useHydration<T> (key: string, get: () => T, set: (value: T) => v
|
|
|
63
63
|
|
|
64
64
|
## Parameters
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
| Parameter | Type | Description |
|
|
67
|
+
| --- | --- | --- |
|
|
68
|
+
| `key` | `string` | A unique key that identifies the data in your Nuxt application. |
|
|
69
|
+
| `get` | `() => T` | A function executed **only on the server** (called when SSR rendering is done) to set the initial value. |
|
|
70
|
+
| `set` | `(value: T) => void` | A function executed **only on the client** (called when initial Vue instance is created) to receive the data. |
|
|
71
|
+
|
|
72
|
+
## Return Values
|
|
73
|
+
|
|
74
|
+
This composable does not return any value.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: useLazyAsyncData
|
|
3
|
+
description: This wrapper around useAsyncData triggers navigation immediately.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`useLazyAsyncData` provides a wrapper around [`useAsyncData`](/docs/4.x/api/composables/use-async-data) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
12
|
+
|
|
13
|
+
::note
|
|
14
|
+
By default, [`useAsyncData`](/docs/4.x/api/composables/use-async-data) blocks navigation until its async handler is resolved. `useLazyAsyncData` allows navigation to occur immediately while data fetching continues in the background.
|
|
15
|
+
::
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```vue [app/pages/index.vue]
|
|
20
|
+
<script setup lang="ts">
|
|
21
|
+
const { status, data: posts } = await useLazyAsyncData('posts', () => $fetch('/api/posts'))
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<template>
|
|
25
|
+
<div>
|
|
26
|
+
<div v-if="status === 'pending'">
|
|
27
|
+
Loading...
|
|
28
|
+
</div>
|
|
29
|
+
<div v-else-if="status === 'error'">
|
|
30
|
+
Error loading posts
|
|
31
|
+
</div>
|
|
32
|
+
<div v-else>
|
|
33
|
+
{{ posts }}
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
</template>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
When using `useLazyAsyncData`, navigation will occur before fetching is complete. This means you must handle `pending` and `error` states directly within your component's template.
|
|
40
|
+
|
|
41
|
+
::warning
|
|
42
|
+
`useLazyAsyncData` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyAsyncData`.
|
|
43
|
+
::
|
|
44
|
+
|
|
45
|
+
## Type
|
|
46
|
+
|
|
47
|
+
```ts [Signature]
|
|
48
|
+
export function useLazyAsyncData<DataT, ErrorT> (
|
|
49
|
+
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
|
50
|
+
options?: AsyncDataOptions<DataT>,
|
|
51
|
+
): AsyncData<DataT, ErrorT>
|
|
52
|
+
|
|
53
|
+
export function useLazyAsyncData<DataT, ErrorT> (
|
|
54
|
+
key: string,
|
|
55
|
+
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
|
56
|
+
options?: AsyncDataOptions<DataT>,
|
|
57
|
+
): AsyncData<DataT, ErrorT>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`useLazyAsyncData` has the same signature as [`useAsyncData`](/docs/4.x/api/composables/use-async-data).
|
|
61
|
+
|
|
62
|
+
## Parameters
|
|
63
|
+
|
|
64
|
+
`useLazyAsyncData` accepts the same parameters as [`useAsyncData`](/docs/4.x/api/composables/use-async-data), with the `lazy` option automatically set to `true`.
|
|
65
|
+
|
|
66
|
+
:read-more{to="/docs/4.x/api/composables/use-async-data#parameters"}
|
|
67
|
+
|
|
68
|
+
## Return Values
|
|
69
|
+
|
|
70
|
+
`useLazyAsyncData` returns the same values as [`useAsyncData`](/docs/4.x/api/composables/use-async-data).
|
|
71
|
+
|
|
72
|
+
:read-more{to="/docs/4.x/api/composables/use-async-data#return-values"}
|
|
73
|
+
|
|
74
|
+
## Example
|
|
75
|
+
|
|
76
|
+
```vue [app/pages/index.vue]
|
|
77
|
+
<script setup lang="ts">
|
|
78
|
+
/* Navigation will occur before fetching is complete.
|
|
79
|
+
Handle 'pending' and 'error' states directly within your component's template
|
|
80
|
+
*/
|
|
81
|
+
const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
|
|
82
|
+
|
|
83
|
+
watch(count, (newCount) => {
|
|
84
|
+
// Because count might start out null, you won't have access
|
|
85
|
+
// to its contents immediately, but you can watch it.
|
|
86
|
+
})
|
|
87
|
+
</script>
|
|
88
|
+
|
|
89
|
+
<template>
|
|
90
|
+
<div>
|
|
91
|
+
{{ status === 'pending' ? 'Loading' : count }}
|
|
92
|
+
</div>
|
|
93
|
+
</template>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'useLazyFetch'
|
|
3
|
+
description: This wrapper around useFetch triggers navigation immediately.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/fetch.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`useLazyFetch` provides a wrapper around [`useFetch`](/docs/4.x/api/composables/use-fetch) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
By default, [`useFetch`](/docs/4.x/api/composables/use-fetch) blocks navigation until its async handler is resolved. `useLazyFetch` allows navigation to proceed immediately, with data being fetched in the background.
|
|
16
|
+
|
|
17
|
+
```vue [app/pages/index.vue]
|
|
18
|
+
<script setup lang="ts">
|
|
19
|
+
const { status, data: posts } = await useLazyFetch('/api/posts')
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<template>
|
|
23
|
+
<div v-if="status === 'pending'">
|
|
24
|
+
Loading ...
|
|
25
|
+
</div>
|
|
26
|
+
<div v-else>
|
|
27
|
+
<div v-for="post in posts">
|
|
28
|
+
<!-- do something -->
|
|
29
|
+
</div>
|
|
30
|
+
</div>
|
|
31
|
+
</template>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
::note
|
|
35
|
+
`useLazyFetch` has the same signature as [`useFetch`](/docs/4.x/api/composables/use-fetch).
|
|
36
|
+
::
|
|
37
|
+
|
|
38
|
+
::warning
|
|
39
|
+
Awaiting `useLazyFetch` only ensures the call is initialized. On client-side navigation, data may not be immediately available, and you must handle the `pending` state in your component's template.
|
|
40
|
+
::
|
|
41
|
+
|
|
42
|
+
::warning
|
|
43
|
+
`useLazyFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyFetch`.
|
|
44
|
+
::
|
|
45
|
+
|
|
46
|
+
## Type
|
|
47
|
+
|
|
48
|
+
```ts [Signature]
|
|
49
|
+
export function useLazyFetch<DataT, ErrorT> (
|
|
50
|
+
url: string | Request | Ref<string | Request> | (() => string | Request),
|
|
51
|
+
options?: UseFetchOptions<DataT>,
|
|
52
|
+
): Promise<AsyncData<DataT, ErrorT>>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
::note
|
|
56
|
+
`useLazyFetch` is equivalent to `useFetch` with `lazy: true` option set. See [`useFetch`](/docs/4.x/api/composables/use-fetch) for full type definitions.
|
|
57
|
+
::
|
|
58
|
+
|
|
59
|
+
## Parameters
|
|
60
|
+
|
|
61
|
+
`useLazyFetch` accepts the same parameters as [`useFetch`](/docs/4.x/api/composables/use-fetch):
|
|
62
|
+
|
|
63
|
+
- `URL` (`string | Request | Ref<string | Request> | () => string | Request`): The URL or request to fetch.
|
|
64
|
+
- `options` (object): Same as [`useFetch` options](/docs/4.x/api/composables/use-fetch#parameters), with `lazy` automatically set to `true`.
|
|
65
|
+
|
|
66
|
+
:read-more{to="/docs/4.x/api/composables/use-fetch#parameters"}
|
|
67
|
+
|
|
68
|
+
## Return Values
|
|
69
|
+
|
|
70
|
+
Returns the same `AsyncData` object as [`useFetch`](/docs/4.x/api/composables/use-fetch):
|
|
71
|
+
|
|
72
|
+
| Name | Type | Description |
|
|
73
|
+
|-----------|-----------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
|
|
74
|
+
| `data` | `Ref<DataT \| undefined>` | The result of the asynchronous fetch. |
|
|
75
|
+
| `refresh` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Function to manually refresh the data. |
|
|
76
|
+
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`. |
|
|
77
|
+
| `error` | `Ref<ErrorT \| undefined>` | Error object if the data fetching failed. |
|
|
78
|
+
| `status` | `Ref<'idle' \| 'pending' \| 'success' \| 'error'>` | Status of the data request. |
|
|
79
|
+
| `clear` | `() => void` | Resets `data` to `undefined`, `error` to `undefined`, sets `status` to `idle`, and cancels any pending requests. |
|
|
80
|
+
|
|
81
|
+
:read-more{to="/docs/4.x/api/composables/use-fetch#return-values"}
|
|
82
|
+
|
|
83
|
+
## Examples
|
|
84
|
+
|
|
85
|
+
### Handling Pending State
|
|
86
|
+
|
|
87
|
+
```vue [app/pages/index.vue]
|
|
88
|
+
<script setup lang="ts">
|
|
89
|
+
/* Navigation will occur before fetching is complete.
|
|
90
|
+
* Handle 'pending' and 'error' states directly within your component's template
|
|
91
|
+
*/
|
|
92
|
+
const { status, data: posts } = await useLazyFetch('/api/posts')
|
|
93
|
+
watch(posts, (newPosts) => {
|
|
94
|
+
// Because posts might start out null, you won't have access
|
|
95
|
+
// to its contents immediately, but you can watch it.
|
|
96
|
+
})
|
|
97
|
+
</script>
|
|
98
|
+
|
|
99
|
+
<template>
|
|
100
|
+
<div v-if="status === 'pending'">
|
|
101
|
+
Loading ...
|
|
102
|
+
</div>
|
|
103
|
+
<div v-else>
|
|
104
|
+
<div v-for="post in posts">
|
|
105
|
+
<!-- do something -->
|
|
106
|
+
</div>
|
|
107
|
+
</div>
|
|
108
|
+
</template>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -30,7 +30,7 @@ By default, the shared runtime context of Nuxt is namespaced under the [`buildId
|
|
|
30
30
|
|
|
31
31
|
### `provide (name, value)`
|
|
32
32
|
|
|
33
|
-
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/4.x/
|
|
33
|
+
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/4.x/directory-structure/app/plugins). Use the `provide` function to create Nuxt plugins to make values and helper methods available in your Nuxt application across all composables and components.
|
|
34
34
|
|
|
35
35
|
`provide` function accepts `name` and `value` parameters.
|
|
36
36
|
|
|
@@ -46,7 +46,7 @@ As you can see in the example above, `$hello` has become the new and custom part
|
|
|
46
46
|
|
|
47
47
|
### `hook(name, cb)`
|
|
48
48
|
|
|
49
|
-
Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue.js composables and [Nuxt plugins](/docs/4.x/
|
|
49
|
+
Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue.js composables and [Nuxt plugins](/docs/4.x/directory-structure/app/plugins) to hook into the rendering lifecycle.
|
|
50
50
|
|
|
51
51
|
`hook` function is useful for adding custom logic by hooking into the rendering lifecycle at a specific point. `hook` function is mostly used when creating Nuxt plugins.
|
|
52
52
|
|
|
@@ -80,12 +80,12 @@ await nuxtApp.callHook('my-plugin:init')
|
|
|
80
80
|
|
|
81
81
|
### `vueApp`
|
|
82
82
|
|
|
83
|
-
`vueApp` is the global Vue.js [application instance](https://vuejs.org/api/application
|
|
83
|
+
`vueApp` is the global Vue.js [application instance](https://vuejs.org/api/application#application-api) that you can access through `nuxtApp`.
|
|
84
84
|
|
|
85
85
|
Some useful methods:
|
|
86
|
-
- [`component()`](https://vuejs.org/api/application
|
|
87
|
-
- [`directive()`](https://vuejs.org/api/application
|
|
88
|
-
- [`use()`](https://vuejs.org/api/application
|
|
86
|
+
- [`component()`](https://vuejs.org/api/application#app-component) - Registers a global component if passing both a name string and a component definition, or retrieves an already registered one if only the name is passed.
|
|
87
|
+
- [`directive()`](https://vuejs.org/api/application#app-directive) - Registers a global custom directive if passing both a name string and a directive definition, or retrieves an already registered one if only the name is passed[(example)](/docs/4.x/directory-structure/app/plugins#vue-directives).
|
|
88
|
+
- [`use()`](https://vuejs.org/api/application#app-use) - Installs a **[Vue.js Plugin](https://vuejs.org/guide/reusability/plugins)** [(example)](/docs/4.x/directory-structure/app/plugins#vue-plugins).
|
|
89
89
|
|
|
90
90
|
:read-more{icon="i-simple-icons-vuedotjs" to="https://vuejs.org/api/application.html#application-api"}
|
|
91
91
|
|
|
@@ -108,7 +108,7 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
108
108
|
::code-group
|
|
109
109
|
```vue [app/app.vue]
|
|
110
110
|
<script setup lang="ts">
|
|
111
|
-
const { data } = await useAsyncData('count', () => $fetch('/api/count'))
|
|
111
|
+
const { data } = await useAsyncData('count', (_nuxtApp, { signal }) => $fetch('/api/count', { signal }))
|
|
112
112
|
</script>
|
|
113
113
|
```
|
|
114
114
|
```ts [server/api/count.ts]
|
|
@@ -67,7 +67,7 @@ Optimistic Updates is a technique where the user interface is updated immediatel
|
|
|
67
67
|
```vue [app/pages/todos.vue]
|
|
68
68
|
<script setup lang="ts">
|
|
69
69
|
// We can access same data later using 'todos' key
|
|
70
|
-
const { data } = await useAsyncData('todos', () => $fetch('/api/todos'))
|
|
70
|
+
const { data } = await useAsyncData('todos', (_nuxtApp, { signal }) => $fetch('/api/todos', { signal }))
|
|
71
71
|
</script>
|
|
72
72
|
```
|
|
73
73
|
|
|
@@ -33,7 +33,7 @@ const { data: forwarded } = await useAsyncData(() => requestFetch('/api/cookies'
|
|
|
33
33
|
|
|
34
34
|
// This will NOT forward anything
|
|
35
35
|
// Result: { cookies: {} }
|
|
36
|
-
const { data: notForwarded } = await useAsyncData(() => $fetch('/api/cookies'))
|
|
36
|
+
const { data: notForwarded } = await useAsyncData((_nuxtApp, { signal }) => $fetch('/api/cookies', { signal }))
|
|
37
37
|
</script>
|
|
38
38
|
```
|
|
39
39
|
|
|
@@ -37,7 +37,7 @@ header.value = 'my-value'
|
|
|
37
37
|
</template>
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
We can use `useResponseHeader` for example in Nuxt [middleware](/docs/4.x/
|
|
40
|
+
We can use `useResponseHeader` for example in Nuxt [middleware](/docs/4.x/directory-structure/app/middleware) to set a response header for all pages.
|
|
41
41
|
|
|
42
42
|
```ts [app/middleware/my-header-middleware.ts]
|
|
43
43
|
export default defineNuxtRouteMiddleware((to, from) => {
|
|
@@ -76,7 +76,7 @@ The `useRoute()` composable should only be used in the setup function of a Vue c
|
|
|
76
76
|
This applies to any composable that uses `useRoute()` internally too.
|
|
77
77
|
::
|
|
78
78
|
|
|
79
|
-
::read-more{to="/docs/4.x/
|
|
79
|
+
::read-more{to="/docs/4.x/directory-structure/app/middleware"}
|
|
80
80
|
Read more about accessing the route in the middleware section.
|
|
81
81
|
::
|
|
82
82
|
|
|
@@ -84,4 +84,4 @@ Read more about accessing the route in the middleware section.
|
|
|
84
84
|
|
|
85
85
|
Browsers don't send [URL fragments](https://url.spec.whatwg.org/#concept-url-fragment) (for example `#foo`) when making requests. So using `route.fullPath` to affect the template can trigger hydration issues because this will include the fragment on client but not the server.
|
|
86
86
|
|
|
87
|
-
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/type-aliases/
|
|
87
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/type-aliases/routelocationnormalizedloaded"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "useRouter"
|
|
3
|
+
description: "The useRouter composable returns the router instance."
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/router.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
```vue [app/pages/index.vue]
|
|
12
|
+
<script setup lang="ts">
|
|
13
|
+
const router = useRouter()
|
|
14
|
+
</script>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If you only need the router instance within your template, use `$router`:
|
|
18
|
+
|
|
19
|
+
```vue [app/pages/index.vue]
|
|
20
|
+
<template>
|
|
21
|
+
<button @click="$router.back()">
|
|
22
|
+
Back
|
|
23
|
+
</button>
|
|
24
|
+
</template>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
If you have a `app/pages/` directory, `useRouter` is identical in behavior to the one provided by `vue-router`.
|
|
28
|
+
|
|
29
|
+
::read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/interfaces/router#Properties-currentRoute-" target="_blank"}
|
|
30
|
+
Read `vue-router` documentation about the `Router` interface.
|
|
31
|
+
::
|
|
32
|
+
|
|
33
|
+
## Basic Manipulation
|
|
34
|
+
|
|
35
|
+
- [`addRoute()`](https://router.vuejs.org/api/interfaces/router#addRoute-): Add a new route to the router instance. `parentName` can be provided to add new route as the child of an existing route.
|
|
36
|
+
- [`removeRoute()`](https://router.vuejs.org/api/interfaces/router#removeRoute-): Remove an existing route by its name.
|
|
37
|
+
- [`getRoutes()`](https://router.vuejs.org/api/interfaces/router#getRoutes-): Get a full list of all the route records.
|
|
38
|
+
- [`hasRoute()`](https://router.vuejs.org/api/interfaces/router#hasRoute-): Checks if a route with a given name exists.
|
|
39
|
+
- [`resolve()`](https://router.vuejs.org/api/interfaces/router#resolve-): Returns the normalized version of a route location. Also includes an `href` property that includes any existing base.
|
|
40
|
+
|
|
41
|
+
```ts [Example]
|
|
42
|
+
const router = useRouter()
|
|
43
|
+
|
|
44
|
+
router.addRoute({ name: 'home', path: '/home', component: Home })
|
|
45
|
+
router.removeRoute('home')
|
|
46
|
+
router.getRoutes()
|
|
47
|
+
router.hasRoute('home')
|
|
48
|
+
router.resolve({ name: 'home' })
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
::note
|
|
52
|
+
`router.addRoute()` adds route details into an array of routes and it is useful while building [Nuxt plugins](/docs/4.x/directory-structure/app/plugins) while `router.push()` on the other hand, triggers a new navigation immediately and it is useful in pages, Vue components and composable.
|
|
53
|
+
::
|
|
54
|
+
|
|
55
|
+
## Based on History API
|
|
56
|
+
|
|
57
|
+
- [`back()`](https://router.vuejs.org/api/interfaces/router#back-): Go back in history if possible, same as `router.go(-1)`.
|
|
58
|
+
- [`forward()`](https://router.vuejs.org/api/interfaces/router#forward-): Go forward in history if possible, same as `router.go(1)`.
|
|
59
|
+
- [`go()`](https://router.vuejs.org/api/interfaces/router#go-): Move forward or backward through the history without the hierarchical restrictions enforced in `router.back()` and `router.forward()`.
|
|
60
|
+
- [`push()`](https://router.vuejs.org/api/interfaces/router#push-): Programmatically navigate to a new URL by pushing an entry in the history stack. **It is recommended to use [`navigateTo`](/docs/4.x/api/utils/navigate-to) instead.**
|
|
61
|
+
- [`replace()`](https://router.vuejs.org/api/interfaces/router#replace-): Programmatically navigate to a new URL by replacing the current entry in the routes history stack. **It is recommended to use [`navigateTo`](/docs/4.x/api/utils/navigate-to) instead.**
|
|
62
|
+
|
|
63
|
+
```ts [Example]
|
|
64
|
+
const router = useRouter()
|
|
65
|
+
|
|
66
|
+
router.back()
|
|
67
|
+
router.forward()
|
|
68
|
+
router.go(3)
|
|
69
|
+
router.push({ path: '/home' })
|
|
70
|
+
router.replace({ hash: '#bio' })
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
::read-more{icon="i-simple-icons-mdnwebdocs" to="https://developer.mozilla.org/en-US/docs/Web/API/History" target="_blank"}
|
|
74
|
+
Read more about the browser's History API.
|
|
75
|
+
::
|
|
76
|
+
|
|
77
|
+
## Navigation Guards
|
|
78
|
+
|
|
79
|
+
`useRouter` composable provides `afterEach`, `beforeEach` and `beforeResolve` helper methods that acts as navigation guards.
|
|
80
|
+
|
|
81
|
+
However, Nuxt has a concept of **route middleware** that simplifies the implementation of navigation guards and provides a better developer experience.
|
|
82
|
+
|
|
83
|
+
:read-more{to="/docs/4.x/directory-structure/app/middleware"}
|
|
84
|
+
|
|
85
|
+
## Promise and Error Handling
|
|
86
|
+
|
|
87
|
+
- [`isReady()`](https://router.vuejs.org/api/interfaces/router#isReady-): Returns a Promise that resolves when the router has completed the initial navigation.
|
|
88
|
+
- [`onError`](https://router.vuejs.org/api/interfaces/router#onError-): Adds an error handler that is called every time a non caught error happens during navigation.
|
|
89
|
+
|
|
90
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/interfaces/router#Methods-" title="Vue Router Docs" target="_blank"}
|
|
91
|
+
|
|
92
|
+
## Universal Router Instance
|
|
93
|
+
|
|
94
|
+
If you do not have a `app/pages/` folder, then [`useRouter`](/docs/4.x/api/composables/use-router) will return a universal router instance with similar helper methods, but be aware that not all features may be supported or behave in exactly the same way as with `vue-router`.
|
|
@@ -95,7 +95,7 @@ Any environment variables set within `.env` file are accessed using `process.env
|
|
|
95
95
|
In **production runtime**, you should use platform environment variables and `.env` is not used.
|
|
96
96
|
::
|
|
97
97
|
|
|
98
|
-
:read-more{to="/docs/4.x/
|
|
98
|
+
:read-more{to="/docs/4.x/directory-structure/env"}
|
|
99
99
|
|
|
100
100
|
## `app` namespace
|
|
101
101
|
|
|
@@ -15,7 +15,7 @@ This composable is available in Nuxt v3.14+.
|
|
|
15
15
|
```ts [signature]
|
|
16
16
|
function useRuntimeHook<THookName extends keyof RuntimeNuxtHooks> (
|
|
17
17
|
name: THookName,
|
|
18
|
-
fn: RuntimeNuxtHooks[THookName] extends HookCallback ? RuntimeNuxtHooks[THookName] : never
|
|
18
|
+
fn: RuntimeNuxtHooks[THookName] extends HookCallback ? RuntimeNuxtHooks[THookName] : never,
|
|
19
19
|
): void
|
|
20
20
|
```
|
|
21
21
|
|