@nuxt/docs 4.1.2 → 4.1.3
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 +6 -6
- package/1.getting-started/02.installation.md +2 -1
- package/1.getting-started/03.configuration.md +36 -36
- package/1.getting-started/04.views.md +9 -9
- package/1.getting-started/05.assets.md +15 -9
- package/1.getting-started/06.styling.md +58 -48
- package/1.getting-started/07.routing.md +17 -17
- package/1.getting-started/08.seo-meta.md +59 -46
- package/1.getting-started/09.transitions.md +49 -44
- package/1.getting-started/10.data-fetching.md +104 -81
- package/1.getting-started/11.state-management.md +26 -19
- package/1.getting-started/12.error-handling.md +22 -20
- package/1.getting-started/13.server.md +8 -8
- package/1.getting-started/14.layers.md +22 -16
- package/1.getting-started/15.prerendering.md +32 -32
- package/1.getting-started/16.deployment.md +10 -10
- package/1.getting-started/17.testing.md +44 -44
- package/1.getting-started/18.upgrade.md +53 -71
- package/2.guide/0.index.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/.navigation.yml +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/0.nuxt.md +3 -3
- package/2.guide/1.directory-structure/0.output.md +18 -0
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/.navigation.yml +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.assets.md +4 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.components.md +45 -28
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.composables.md +13 -13
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.layouts.md +19 -15
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.middleware.md +31 -25
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.pages.md +39 -37
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.plugins.md +25 -25
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.utils.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app-config.md +18 -18
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.error.md +6 -6
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.content.md +7 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.modules.md +8 -8
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.node_modules.md +2 -2
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.public.md +2 -2
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.server.md +35 -35
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.shared.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.env.md +8 -8
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.gitignore.md +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtignore.md +5 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtrc.md +4 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.nuxt-config.md +3 -3
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.package.md +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.tsconfig.md +3 -3
- package/2.guide/{1.concepts → 2.concepts}/1.auto-imports.md +22 -22
- package/2.guide/{1.concepts → 2.concepts}/10.nuxt-lifecycle.md +17 -17
- package/2.guide/{1.concepts → 2.concepts}/2.vuejs-development.md +13 -13
- package/2.guide/{1.concepts → 2.concepts}/3.rendering.md +24 -22
- package/2.guide/{1.concepts → 2.concepts}/4.server-engine.md +6 -6
- package/2.guide/{1.concepts → 2.concepts}/5.modules.md +5 -5
- package/2.guide/{1.concepts → 2.concepts}/7.esm.md +17 -15
- package/2.guide/{1.concepts → 2.concepts}/8.typescript.md +12 -12
- package/2.guide/3.going-further/1.events.md +3 -3
- package/2.guide/3.going-further/1.experimental-features.md +246 -85
- package/2.guide/3.going-further/1.features.md +44 -9
- package/2.guide/3.going-further/1.internals.md +25 -25
- package/2.guide/3.going-further/10.runtime-config.md +12 -12
- package/2.guide/3.going-further/11.nightly-release-channel.md +1 -1
- package/2.guide/3.going-further/2.hooks.md +14 -14
- package/2.guide/3.going-further/3.modules.md +96 -94
- package/2.guide/3.going-further/4.kit.md +7 -7
- package/2.guide/3.going-further/6.nuxt-app.md +8 -8
- package/2.guide/3.going-further/7.layers.md +62 -53
- package/2.guide/3.going-further/9.debugging.md +2 -2
- package/2.guide/4.recipes/1.custom-routing.md +30 -30
- package/2.guide/4.recipes/2.vite-plugin.md +4 -4
- package/2.guide/4.recipes/3.custom-usefetch.md +13 -13
- package/2.guide/4.recipes/4.sessions-and-authentication.md +35 -21
- package/2.guide/5.best-practices/hydration.md +4 -4
- package/2.guide/5.best-practices/performance.md +17 -17
- package/3.api/1.components/1.client-only.md +6 -3
- package/3.api/1.components/1.nuxt-client-fallback.md +10 -7
- package/3.api/1.components/10.nuxt-picture.md +1 -1
- package/3.api/1.components/11.teleports.md +5 -2
- package/3.api/1.components/12.nuxt-route-announcer.md +9 -9
- package/3.api/1.components/13.nuxt-time.md +44 -17
- package/3.api/1.components/2.nuxt-page.md +6 -6
- package/3.api/1.components/3.nuxt-layout.md +15 -10
- package/3.api/1.components/4.nuxt-link.md +42 -22
- package/3.api/1.components/5.nuxt-loading-indicator.md +3 -3
- package/3.api/1.components/6.nuxt-error-boundary.md +12 -10
- package/3.api/2.composables/on-prehydrate.md +2 -2
- package/3.api/2.composables/use-app-config.md +1 -1
- package/3.api/2.composables/use-async-data.md +20 -20
- package/3.api/2.composables/use-cookie.md +29 -21
- package/3.api/2.composables/use-error.md +2 -2
- package/3.api/2.composables/use-fetch.md +58 -32
- package/3.api/2.composables/use-head-safe.md +7 -7
- package/3.api/2.composables/use-head.md +5 -5
- package/3.api/2.composables/use-hydration.md +6 -6
- package/3.api/2.composables/use-lazy-async-data.md +4 -4
- package/3.api/2.composables/use-lazy-fetch.md +4 -4
- package/3.api/2.composables/use-loading-indicator.md +12 -12
- package/3.api/2.composables/use-nuxt-app.md +22 -22
- package/3.api/2.composables/use-nuxt-data.md +8 -8
- package/3.api/2.composables/use-preview-mode.md +15 -18
- package/3.api/2.composables/use-request-event.md +1 -1
- package/3.api/2.composables/use-request-fetch.md +3 -3
- package/3.api/2.composables/use-request-header.md +1 -1
- package/3.api/2.composables/use-request-headers.md +4 -4
- package/3.api/2.composables/use-request-url.md +1 -1
- package/3.api/2.composables/use-response-header.md +9 -10
- package/3.api/2.composables/use-route-announcer.md +4 -4
- package/3.api/2.composables/use-route.md +1 -1
- package/3.api/2.composables/use-router.md +10 -8
- package/3.api/2.composables/use-runtime-config.md +11 -11
- package/3.api/2.composables/use-runtime-hook.md +2 -2
- package/3.api/2.composables/use-seo-meta.md +4 -4
- package/3.api/2.composables/use-server-seo-meta.md +6 -6
- package/3.api/2.composables/use-state.md +5 -5
- package/3.api/3.utils/$fetch.md +10 -8
- package/3.api/3.utils/abort-navigation.md +3 -3
- package/3.api/3.utils/add-route-middleware.md +5 -5
- package/3.api/3.utils/call-once.md +6 -6
- package/3.api/3.utils/clear-error.md +3 -3
- package/3.api/3.utils/clear-nuxt-data.md +3 -3
- package/3.api/3.utils/clear-nuxt-state.md +3 -3
- package/3.api/3.utils/create-error.md +2 -2
- package/3.api/3.utils/define-lazy-hydration-component.md +13 -13
- package/3.api/3.utils/define-nuxt-component.md +5 -5
- package/3.api/3.utils/define-nuxt-plugin.md +13 -13
- package/3.api/3.utils/define-nuxt-route-middleware.md +5 -5
- package/3.api/3.utils/define-page-meta.md +23 -23
- package/3.api/3.utils/define-route-rules.md +7 -7
- package/3.api/3.utils/navigate-to.md +11 -11
- package/3.api/3.utils/prefetch-components.md +1 -1
- package/3.api/3.utils/preload-components.md +1 -1
- package/3.api/3.utils/preload-route-components.md +2 -2
- package/3.api/3.utils/prerender-routes.md +3 -3
- package/3.api/3.utils/refresh-cookie.md +4 -4
- package/3.api/3.utils/refresh-nuxt-data.md +12 -7
- package/3.api/3.utils/reload-nuxt-app.md +3 -3
- package/3.api/3.utils/set-page-layout.md +1 -1
- package/3.api/3.utils/set-response-status.md +3 -3
- package/3.api/3.utils/show-error.md +5 -5
- package/3.api/3.utils/update-app-config.md +4 -3
- package/3.api/4.commands/add.md +1 -1
- package/3.api/4.commands/analyze.md +2 -1
- package/3.api/4.commands/build.md +2 -1
- package/3.api/4.commands/dev.md +5 -4
- package/3.api/4.commands/generate.md +3 -2
- package/3.api/4.commands/init.md +3 -2
- package/3.api/4.commands/module.md +4 -4
- package/3.api/4.commands/prepare.md +7 -2
- package/3.api/4.commands/preview.md +5 -4
- package/3.api/4.commands/test.md +40 -0
- package/3.api/4.commands/typecheck.md +5 -3
- package/3.api/4.commands/upgrade.md +3 -3
- package/3.api/5.kit/1.modules.md +36 -36
- package/3.api/5.kit/10.runtime-config.md +1 -1
- package/3.api/5.kit/10.templates.md +8 -6
- package/3.api/5.kit/11.nitro.md +62 -62
- package/3.api/5.kit/12.resolving.md +2 -2
- package/3.api/5.kit/14.builder.md +1 -0
- package/3.api/5.kit/15.examples.md +2 -2
- package/3.api/5.kit/16.layers.md +26 -26
- package/3.api/5.kit/3.compatibility.md +14 -14
- package/3.api/5.kit/4.autoimports.md +13 -13
- package/3.api/5.kit/5.components.md +7 -7
- package/3.api/5.kit/6.context.md +3 -3
- package/3.api/5.kit/7.pages.md +7 -7
- package/3.api/5.kit/8.layout.md +2 -2
- package/3.api/5.kit/9.plugins.md +5 -4
- package/3.api/6.advanced/1.hooks.md +3 -3
- package/3.api/6.advanced/2.import-meta.md +3 -3
- package/3.api/6.nuxt-config.md +300 -865
- package/3.api/index.md +7 -7
- package/5.community/2.getting-help.md +1 -1
- package/5.community/3.reporting-bugs.md +1 -1
- package/5.community/4.contribution.md +11 -11
- package/5.community/5.framework-contribution.md +4 -4
- package/5.community/6.roadmap.md +1 -1
- package/6.bridge/1.overview.md +13 -13
- package/6.bridge/10.configuration.md +2 -1
- package/6.bridge/2.typescript.md +3 -3
- package/6.bridge/3.bridge-composition-api.md +8 -8
- package/6.bridge/4.plugins-and-middleware.md +9 -9
- package/6.bridge/5.nuxt3-compatible-api.md +20 -17
- package/6.bridge/6.meta.md +20 -19
- package/6.bridge/7.runtime-config.md +1 -1
- package/6.bridge/8.nitro.md +3 -3
- package/6.bridge/9.vite.md +4 -4
- package/7.migration/1.overview.md +2 -2
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +23 -21
- package/7.migration/20.module-authors.md +7 -7
- package/7.migration/3.auto-imports.md +3 -3
- package/7.migration/4.meta.md +21 -18
- package/7.migration/5.plugins-and-middleware.md +8 -8
- package/7.migration/6.pages-and-layouts.md +28 -24
- package/7.migration/7.component-options.md +18 -18
- package/7.migration/8.runtime-config.md +7 -7
- package/package.json +1 -1
- package/2.guide/2.directory-structure/0.output.md +0 -18
- /package/2.guide/{1.concepts → 2.concepts}/.navigation.yml +0 -0
- /package/2.guide/{1.concepts → 2.concepts}/9.code-style.md +0 -0
|
@@ -10,17 +10,17 @@ links:
|
|
|
10
10
|
|
|
11
11
|
## Description
|
|
12
12
|
|
|
13
|
-
By default, [`useFetch`](/docs/api/composables/use-fetch) blocks navigation until its async handler is resolved. `useLazyFetch` provides a wrapper around [`useFetch`](/docs/api/composables/use-fetch) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
13
|
+
By default, [`useFetch`](/docs/4.x/api/composables/use-fetch) blocks navigation until its async handler is resolved. `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`.
|
|
14
14
|
|
|
15
15
|
::note
|
|
16
|
-
`useLazyFetch` has the same signature as [`useFetch`](/docs/api/composables/use-fetch).
|
|
16
|
+
`useLazyFetch` has the same signature as [`useFetch`](/docs/4.x/api/composables/use-fetch).
|
|
17
17
|
::
|
|
18
18
|
|
|
19
19
|
::note
|
|
20
20
|
Awaiting `useLazyFetch` in this mode only ensures the call is initialized. On client-side navigation, data may not be immediately available, and you should make sure to handle the pending state in your app.
|
|
21
21
|
::
|
|
22
22
|
|
|
23
|
-
:read-more{to="/docs/api/composables/use-fetch"}
|
|
23
|
+
:read-more{to="/docs/4.x/api/composables/use-fetch"}
|
|
24
24
|
|
|
25
25
|
## Example
|
|
26
26
|
|
|
@@ -52,4 +52,4 @@ watch(posts, (newPosts) => {
|
|
|
52
52
|
`useLazyFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyFetch`.
|
|
53
53
|
::
|
|
54
54
|
|
|
55
|
-
:read-more{to="/docs/getting-started/data-fetching"}
|
|
55
|
+
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -10,8 +10,8 @@ links:
|
|
|
10
10
|
|
|
11
11
|
## Description
|
|
12
12
|
|
|
13
|
-
A composable which returns the loading state of the page. Used by [`<NuxtLoadingIndicator>`](/docs/api/components/nuxt-loading-indicator) and controllable.
|
|
14
|
-
It hooks into [`page:loading:start`](/docs/api/advanced/hooks#app-hooks-runtime) and [`page:loading:end`](/docs/api/advanced/hooks#app-hooks-runtime) to change its state.
|
|
13
|
+
A composable which returns the loading state of the page. Used by [`<NuxtLoadingIndicator>`](/docs/4.x/api/components/nuxt-loading-indicator) and controllable.
|
|
14
|
+
It hooks into [`page:loading:start`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) and [`page:loading:end`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) to change its state.
|
|
15
15
|
|
|
16
16
|
## Parameters
|
|
17
17
|
|
|
@@ -58,20 +58,20 @@ Used by `finish()`. Clear all timers and intervals used by the composable.
|
|
|
58
58
|
|
|
59
59
|
```vue
|
|
60
60
|
<script setup lang="ts">
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const { progress, isLoading, start, finish, clear } = useLoadingIndicator({
|
|
62
|
+
duration: 2000,
|
|
63
|
+
throttle: 200,
|
|
64
|
+
// This is how progress is calculated by default
|
|
65
|
+
estimatedProgress: (duration, elapsed) => (2 / Math.PI * 100) * Math.atan(elapsed / duration * 100 / 50),
|
|
66
|
+
})
|
|
67
67
|
</script>
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
```vue
|
|
71
71
|
<script setup lang="ts">
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
const { start, set } = useLoadingIndicator()
|
|
73
|
+
// same as set(0, { force: true })
|
|
74
|
+
// set the progress to 0, and show loading immediately
|
|
75
|
+
start({ force: true })
|
|
76
76
|
</script>
|
|
77
77
|
```
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
`useNuxtApp` is a built-in composable that provides a way to access shared runtime context of Nuxt, also known as the [Nuxt context](/docs/guide/going-further/nuxt-app#the-nuxt-context), which is available on both client and server side (but not within Nitro routes). It helps you access the Vue app instance, runtime hooks, runtime config variables and internal states, such as `ssrContext` and `payload`.
|
|
11
|
+
`useNuxtApp` is a built-in composable that provides a way to access shared runtime context of Nuxt, also known as the [Nuxt context](/docs/4.x/guide/going-further/nuxt-app#the-nuxt-context), which is available on both client and server side (but not within Nitro routes). It helps you access the Vue app instance, runtime hooks, runtime config variables and internal states, such as `ssrContext` and `payload`.
|
|
12
12
|
|
|
13
13
|
```vue [app/app.vue]
|
|
14
14
|
<script setup lang="ts">
|
|
@@ -16,11 +16,11 @@ const nuxtApp = useNuxtApp()
|
|
|
16
16
|
</script>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
If runtime context is unavailable in your scope, `useNuxtApp` will throw an exception when called. You can use [`tryUseNuxtApp`](#tryusenuxtapp) instead for composables that do not require `nuxtApp`, or to simply check if context is available or not without an exception.
|
|
19
|
+
If runtime context is unavailable in your scope, `useNuxtApp` will throw an exception when called. You can use [`tryUseNuxtApp`](/docs/4.x/api/composables/use-nuxt-app#tryusenuxtapp) instead for composables that do not require `nuxtApp`, or to simply check if context is available or not without an exception.
|
|
20
20
|
|
|
21
21
|
<!--
|
|
22
22
|
note
|
|
23
|
-
By default, the shared runtime context of Nuxt is namespaced under the [`buildId`](/docs/api/nuxt-config#buildid) option. It allows the support of multiple runtime contexts.
|
|
23
|
+
By default, the shared runtime context of Nuxt is namespaced under the [`buildId`](/docs/4.x/api/nuxt-config#buildid) option. It allows the support of multiple runtime contexts.
|
|
24
24
|
|
|
25
25
|
## Params
|
|
26
26
|
|
|
@@ -30,13 +30,13 @@ 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/guide/directory-structure/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.
|
|
33
|
+
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/4.x/guide/directory-structure/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
|
|
|
37
|
-
```
|
|
37
|
+
```ts
|
|
38
38
|
const nuxtApp = useNuxtApp()
|
|
39
|
-
nuxtApp.provide('hello',
|
|
39
|
+
nuxtApp.provide('hello', name => `Hello ${name}!`)
|
|
40
40
|
|
|
41
41
|
// Prints "Hello name!"
|
|
42
42
|
console.log(nuxtApp.$hello('name'))
|
|
@@ -46,11 +46,11 @@ 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/guide/directory-structure/plugins) to hook into the rendering lifecycle.
|
|
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/guide/directory-structure/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
|
|
|
53
|
-
See [Runtime Hooks](/docs/api/advanced/hooks#app-hooks-runtime) for available runtime hooks called by Nuxt.
|
|
53
|
+
See [Runtime Hooks](/docs/4.x/api/advanced/hooks#app-hooks-runtime) for available runtime hooks called by Nuxt.
|
|
54
54
|
|
|
55
55
|
```ts [app/plugins/test.ts]
|
|
56
56
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
@@ -84,8 +84,8 @@ await nuxtApp.callHook('my-plugin:init')
|
|
|
84
84
|
|
|
85
85
|
Some useful methods:
|
|
86
86
|
- [`component()`](https://vuejs.org/api/application.html#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.html#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/guide/directory-structure/plugins#vue-directives).
|
|
88
|
-
- [`use()`](https://vuejs.org/api/application.html#app-use) - Installs a **[Vue.js Plugin](https://vuejs.org/guide/reusability/plugins.html)** [(example)](/docs/guide/directory-structure/plugins#vue-plugins).
|
|
87
|
+
- [`directive()`](https://vuejs.org/api/application.html#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/guide/directory-structure/plugins#vue-directives).
|
|
88
|
+
- [`use()`](https://vuejs.org/api/application.html#app-use) - Installs a **[Vue.js Plugin](https://vuejs.org/guide/reusability/plugins.html)** [(example)](/docs/4.x/guide/directory-structure/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
|
|
|
@@ -103,7 +103,7 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
103
103
|
`payload` exposes data and state variables from server side to client side. The following keys will be available on the client after they have been passed from the server side:
|
|
104
104
|
|
|
105
105
|
- `serverRendered` (boolean) - Indicates if response is server-side-rendered.
|
|
106
|
-
- `data` (object) - When you fetch the data from an API endpoint using either [`useFetch`](/docs/api/composables/use-fetch) or [`useAsyncData`](/docs/api/composables/use-async-data) , resulting payload can be accessed from the `payload.data`. This data is cached and helps you prevent fetching the same data in case an identical request is made more than once.
|
|
106
|
+
- `data` (object) - When you fetch the data from an API endpoint using either [`useFetch`](/docs/4.x/api/composables/use-fetch) or [`useAsyncData`](/docs/4.x/api/composables/use-async-data) , resulting payload can be accessed from the `payload.data`. This data is cached and helps you prevent fetching the same data in case an identical request is made more than once.
|
|
107
107
|
|
|
108
108
|
::code-group
|
|
109
109
|
```vue [app/app.vue]
|
|
@@ -112,17 +112,17 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
112
112
|
</script>
|
|
113
113
|
```
|
|
114
114
|
```ts [server/api/count.ts]
|
|
115
|
-
export default defineEventHandler(event => {
|
|
115
|
+
export default defineEventHandler((event) => {
|
|
116
116
|
return { count: 1 }
|
|
117
117
|
})
|
|
118
118
|
```
|
|
119
119
|
::
|
|
120
120
|
|
|
121
|
-
After fetching the value of `count` using [`useAsyncData`](/docs/api/composables/use-async-data) in the example above, if you access `payload.data`, you will see `{ count: 1 }` recorded there.
|
|
121
|
+
After fetching the value of `count` using [`useAsyncData`](/docs/4.x/api/composables/use-async-data) in the example above, if you access `payload.data`, you will see `{ count: 1 }` recorded there.
|
|
122
122
|
|
|
123
|
-
When accessing the same `payload.data` from [`ssrcontext`](#ssrcontext), you can access the same value on the server side as well.
|
|
123
|
+
When accessing the same `payload.data` from [`ssrcontext`](/docs/4.x/api/composables/use-nuxt-app#ssrcontext), you can access the same value on the server side as well.
|
|
124
124
|
|
|
125
|
-
- `state` (object) - When you use [`useState`](/docs/api/composables/use-state) composable in Nuxt to set shared state, this state data is accessed through `payload.state.[name-of-your-state]`.
|
|
125
|
+
- `state` (object) - When you use [`useState`](/docs/4.x/api/composables/use-state) composable in Nuxt to set shared state, this state data is accessed through `payload.state.[name-of-your-state]`.
|
|
126
126
|
|
|
127
127
|
```ts [app/plugins/my-plugin.ts]
|
|
128
128
|
export const useColor = () => useState<string>('color', () => 'pink')
|
|
@@ -173,7 +173,7 @@ export default defineComponent({
|
|
|
173
173
|
// ...
|
|
174
174
|
}
|
|
175
175
|
})
|
|
176
|
-
}
|
|
176
|
+
},
|
|
177
177
|
})
|
|
178
178
|
```
|
|
179
179
|
|
|
@@ -204,7 +204,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
|
204
204
|
|
|
205
205
|
#### Usage
|
|
206
206
|
|
|
207
|
-
```
|
|
207
|
+
```ts
|
|
208
208
|
const result = nuxtApp.runWithContext(() => functionWithContext())
|
|
209
209
|
```
|
|
210
210
|
|
|
@@ -218,14 +218,14 @@ Vue.js Composition API (and Nuxt composables similarly) work by depending on an
|
|
|
218
218
|
|
|
219
219
|
What it does mean? The Composition API and Nuxt Composables are only available during lifecycle and in same tick before any async operation:
|
|
220
220
|
|
|
221
|
-
```
|
|
221
|
+
```ts
|
|
222
222
|
// --- Vue internal ---
|
|
223
223
|
const _vueInstance = null
|
|
224
224
|
const getCurrentInstance = () => _vueInstance
|
|
225
225
|
// ---
|
|
226
226
|
|
|
227
227
|
// Vue / Nuxt sets a global variable referencing to current component in _vueInstance when calling setup()
|
|
228
|
-
async function setup() {
|
|
228
|
+
async function setup () {
|
|
229
229
|
getCurrentInstance() // Works
|
|
230
230
|
await someAsyncOperation() // Vue unsets the context in same tick before async operation!
|
|
231
231
|
getCurrentInstance() // null
|
|
@@ -236,7 +236,7 @@ The classic solution to this, is caching the current instance on first call to a
|
|
|
236
236
|
|
|
237
237
|
To overcome this limitation, Vue does some behind the scenes work when compiling our application code and restores context after each call for `<script setup>`:
|
|
238
238
|
|
|
239
|
-
```
|
|
239
|
+
```ts
|
|
240
240
|
const __instance = getCurrentInstance() // Generated by Vue compiler
|
|
241
241
|
getCurrentInstance() // Works!
|
|
242
242
|
await someAsyncOperation() // Vue unsets the context
|
|
@@ -268,7 +268,7 @@ Using a new experimental feature, it is possible to enable native async context
|
|
|
268
268
|
Native async context support works currently in Bun and Node.
|
|
269
269
|
::
|
|
270
270
|
|
|
271
|
-
:read-more{to="/docs/guide/going-further/experimental-features#asynccontext"}
|
|
271
|
+
:read-more{to="/docs/4.x/guide/going-further/experimental-features#asynccontext"}
|
|
272
272
|
|
|
273
273
|
## tryUseNuxtApp
|
|
274
274
|
|
|
@@ -279,7 +279,7 @@ You can use it for composables that do not require `nuxtApp`, or to simply check
|
|
|
279
279
|
Example usage:
|
|
280
280
|
|
|
281
281
|
```ts [composable.ts]
|
|
282
|
-
export function useStandType() {
|
|
282
|
+
export function useStandType () {
|
|
283
283
|
// Always works on the client
|
|
284
284
|
if (tryUseNuxtApp()) {
|
|
285
285
|
return useRuntimeConfig().public.STAND_TYPE
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
::note
|
|
12
|
-
`useNuxtData` gives you access to the current cached value of [`useAsyncData`](/docs/api/composables/use-async-data) , [`useLazyAsyncData`](/docs/api/composables/use-lazy-async-data), [`useFetch`](/docs/api/composables/use-fetch) and [`useLazyFetch`](/docs/api/composables/use-lazy-fetch) with explicitly provided key.
|
|
12
|
+
`useNuxtData` gives you access to the current cached value of [`useAsyncData`](/docs/4.x/api/composables/use-async-data) , [`useLazyAsyncData`](/docs/4.x/api/composables/use-lazy-async-data), [`useFetch`](/docs/4.x/api/composables/use-fetch) and [`useLazyFetch`](/docs/4.x/api/composables/use-lazy-fetch) with explicitly provided key.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
@@ -50,10 +50,10 @@ const route = useRoute()
|
|
|
50
50
|
|
|
51
51
|
const { data } = useLazyFetch(`/api/posts/${route.params.id}`, {
|
|
52
52
|
key: `post-${route.params.id}`,
|
|
53
|
-
default() {
|
|
53
|
+
default () {
|
|
54
54
|
// Find the individual post from the cache and set it as the default value.
|
|
55
55
|
return posts.value.find(post => post.id === route.params.id)
|
|
56
|
-
}
|
|
56
|
+
},
|
|
57
57
|
})
|
|
58
58
|
</script>
|
|
59
59
|
```
|
|
@@ -80,10 +80,10 @@ let previousTodos = []
|
|
|
80
80
|
const { data: todos } = useNuxtData('todos')
|
|
81
81
|
|
|
82
82
|
async function addTodo () {
|
|
83
|
-
|
|
83
|
+
await $fetch('/api/addTodo', {
|
|
84
84
|
method: 'post',
|
|
85
85
|
body: {
|
|
86
|
-
todo: newTodo.value
|
|
86
|
+
todo: newTodo.value,
|
|
87
87
|
},
|
|
88
88
|
onRequest () {
|
|
89
89
|
// Store the previously cached value to restore if fetch fails.
|
|
@@ -99,7 +99,7 @@ async function addTodo () {
|
|
|
99
99
|
async onResponse () {
|
|
100
100
|
// Invalidate todos in the background if the request succeeded.
|
|
101
101
|
await refreshNuxtData('todos')
|
|
102
|
-
}
|
|
102
|
+
},
|
|
103
103
|
})
|
|
104
104
|
}
|
|
105
105
|
</script>
|
|
@@ -107,6 +107,6 @@ async function addTodo () {
|
|
|
107
107
|
|
|
108
108
|
## Type
|
|
109
109
|
|
|
110
|
-
```ts
|
|
111
|
-
useNuxtData<DataT = any> (key: string): { data: Ref<DataT | undefined> }
|
|
110
|
+
```ts [Signature]
|
|
111
|
+
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | undefined> }
|
|
112
112
|
```
|
|
@@ -12,9 +12,9 @@ links:
|
|
|
12
12
|
|
|
13
13
|
Preview mode allows you to see how your changes would be displayed on a live site without revealing them to users.
|
|
14
14
|
|
|
15
|
-
You can use the built-in `usePreviewMode` composable to access and control preview state in Nuxt. If the composable detects preview mode it will automatically force any updates necessary for [`useAsyncData`](/docs/api/composables/use-async-data) and [`useFetch`](/docs/api/composables/use-fetch) to rerender preview content.
|
|
15
|
+
You can use the built-in `usePreviewMode` composable to access and control preview state in Nuxt. If the composable detects preview mode it will automatically force any updates necessary for [`useAsyncData`](/docs/4.x/api/composables/use-async-data) and [`useFetch`](/docs/4.x/api/composables/use-fetch) to rerender preview content.
|
|
16
16
|
|
|
17
|
-
```
|
|
17
|
+
```ts
|
|
18
18
|
const { enabled, state } = usePreviewMode()
|
|
19
19
|
```
|
|
20
20
|
|
|
@@ -24,27 +24,28 @@ const { enabled, state } = usePreviewMode()
|
|
|
24
24
|
|
|
25
25
|
You can specify a custom way to enable preview mode. By default the `usePreviewMode` composable will enable preview mode if there is a `preview` param in url that is equal to `true` (for example, `http://localhost:3000?preview=true`). You can wrap the `usePreviewMode` into custom composable, to keep options consistent across usages and prevent any errors.
|
|
26
26
|
|
|
27
|
-
```
|
|
27
|
+
```ts
|
|
28
28
|
export function useMyPreviewMode () {
|
|
29
|
+
const route = useRoute()
|
|
29
30
|
return usePreviewMode({
|
|
30
31
|
shouldEnable: () => {
|
|
31
32
|
return !!route.query.customPreview
|
|
32
|
-
}
|
|
33
|
-
})
|
|
33
|
+
},
|
|
34
|
+
})
|
|
34
35
|
}
|
|
35
36
|
```
|
|
36
37
|
|
|
37
38
|
### Modify default state
|
|
38
39
|
|
|
39
|
-
`usePreviewMode` will try to store the value of a `token` param from url in state. You can modify this state and it will be available for all [`usePreviewMode`](/docs/api/composables/use-preview-mode) calls.
|
|
40
|
+
`usePreviewMode` will try to store the value of a `token` param from url in state. You can modify this state and it will be available for all [`usePreviewMode`](/docs/4.x/api/composables/use-preview-mode) calls.
|
|
40
41
|
|
|
41
|
-
```
|
|
42
|
+
```ts
|
|
42
43
|
const data1 = ref('data1')
|
|
43
44
|
|
|
44
45
|
const { enabled, state } = usePreviewMode({
|
|
45
46
|
getState: (currentState) => {
|
|
46
47
|
return { data1, data2: 'data2' }
|
|
47
|
-
}
|
|
48
|
+
},
|
|
48
49
|
})
|
|
49
50
|
```
|
|
50
51
|
|
|
@@ -60,14 +61,14 @@ When preview mode is disabled, the composable will attach a callback to call `re
|
|
|
60
61
|
|
|
61
62
|
You can specify custom callbacks to be triggered by providing your own functions for the `onEnable` and `onDisable` options.
|
|
62
63
|
|
|
63
|
-
```
|
|
64
|
+
```ts
|
|
64
65
|
const { enabled, state } = usePreviewMode({
|
|
65
66
|
onEnable: () => {
|
|
66
67
|
console.log('preview mode has been enabled')
|
|
67
68
|
},
|
|
68
69
|
onDisable: () => {
|
|
69
70
|
console.log('preview mode has been disabled')
|
|
70
|
-
}
|
|
71
|
+
},
|
|
71
72
|
})
|
|
72
73
|
```
|
|
73
74
|
|
|
@@ -81,8 +82,8 @@ const { enabled, state } = usePreviewMode()
|
|
|
81
82
|
|
|
82
83
|
const { data } = await useFetch('/api/preview', {
|
|
83
84
|
query: {
|
|
84
|
-
apiKey: state.token
|
|
85
|
-
}
|
|
85
|
+
apiKey: state.token,
|
|
86
|
+
},
|
|
86
87
|
})
|
|
87
88
|
</script>
|
|
88
89
|
|
|
@@ -107,12 +108,8 @@ npx nuxt generate
|
|
|
107
108
|
npx nuxt preview
|
|
108
109
|
```
|
|
109
110
|
|
|
110
|
-
Then you can see your preview page by adding the query param `preview` to the end of the page you want to see once:
|
|
111
|
-
|
|
112
|
-
```js
|
|
113
|
-
?preview=true
|
|
114
|
-
```
|
|
111
|
+
Then you can see your preview page by adding the query param `preview` to the end of the page you want to see once, for example `http://localhost:3000/?preview=true`.
|
|
115
112
|
|
|
116
113
|
::note
|
|
117
|
-
`usePreviewMode` should be tested locally with `nuxt generate` and then `nuxt preview` rather than `nuxt dev`. (The [preview command](/docs/api/commands/preview) is not related to preview mode.)
|
|
114
|
+
`usePreviewMode` should be tested locally with `nuxt generate` and then `nuxt preview` rather than `nuxt dev`. (The [preview command](/docs/4.x/api/commands/preview) is not related to preview mode.)
|
|
118
115
|
::
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
Within the [Nuxt context](/docs/guide/going-further/nuxt-app#the-nuxt-context) you can use `useRequestEvent` to access the incoming request.
|
|
11
|
+
Within the [Nuxt context](/docs/4.x/guide/going-further/nuxt-app#the-nuxt-context) you can use `useRequestEvent` to access the incoming request.
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
// Get underlying request event
|
|
@@ -19,7 +19,7 @@ Headers that are **not meant to be forwarded** will **not be included** in the r
|
|
|
19
19
|
::
|
|
20
20
|
|
|
21
21
|
::tip
|
|
22
|
-
The [`useFetch`](/docs/api/composables/use-fetch) composable uses `useRequestFetch` under the hood to automatically forward the request context and headers.
|
|
22
|
+
The [`useFetch`](/docs/4.x/api/composables/use-fetch) composable uses `useRequestFetch` under the hood to automatically forward the request context and headers.
|
|
23
23
|
::
|
|
24
24
|
|
|
25
25
|
::code-group
|
|
@@ -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(() => $fetch('/api/cookies'))
|
|
37
37
|
</script>
|
|
38
38
|
```
|
|
39
39
|
|
|
@@ -48,5 +48,5 @@ export default defineEventHandler((event) => {
|
|
|
48
48
|
::
|
|
49
49
|
|
|
50
50
|
::tip
|
|
51
|
-
In the browser during client-side navigation, `useRequestFetch` will behave just like regular [`$fetch`](/docs/api/utils/dollarfetch).
|
|
51
|
+
In the browser during client-side navigation, `useRequestFetch` will behave just like regular [`$fetch`](/docs/4.x/api/utils/dollarfetch).
|
|
52
52
|
::
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
You can use the built-in [`useRequestHeader`](/docs/api/composables/use-request-header) composable to access any incoming request header within your pages, components, and plugins.
|
|
11
|
+
You can use the built-in [`useRequestHeader`](/docs/4.x/api/composables/use-request-header) composable to access any incoming request header within your pages, components, and plugins.
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
// Get the authorization request header
|
|
@@ -8,14 +8,14 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
You can use built-in [`useRequestHeaders`](/docs/api/composables/use-request-headers) composable to access the incoming request headers within your pages, components, and plugins.
|
|
11
|
+
You can use built-in [`useRequestHeaders`](/docs/4.x/api/composables/use-request-headers) composable to access the incoming request headers within your pages, components, and plugins.
|
|
12
12
|
|
|
13
|
-
```
|
|
13
|
+
```ts
|
|
14
14
|
// Get all request headers
|
|
15
15
|
const headers = useRequestHeaders()
|
|
16
16
|
|
|
17
17
|
// Get only cookie request header
|
|
18
|
-
const
|
|
18
|
+
const { cookie } = useRequestHeaders(['cookie'])
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
::tip
|
|
@@ -31,7 +31,7 @@ The example below adds the `authorization` request header to an isomorphic `$fet
|
|
|
31
31
|
```vue [app/pages/some-page.vue]
|
|
32
32
|
<script setup lang="ts">
|
|
33
33
|
const { data } = await useFetch('/api/confidential', {
|
|
34
|
-
headers: useRequestHeaders(['authorization'])
|
|
34
|
+
headers: useRequestHeaders(['authorization']),
|
|
35
35
|
})
|
|
36
36
|
</script>
|
|
37
37
|
```
|
|
@@ -11,7 +11,7 @@ links:
|
|
|
11
11
|
`useRequestURL` is a helper function that returns an [URL object](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) working on both server-side and client-side.
|
|
12
12
|
|
|
13
13
|
::important
|
|
14
|
-
When utilizing [Hybrid Rendering](/docs/guide/concepts/rendering#hybrid-rendering) with cache strategies, all incoming request headers are dropped when handling the cached responses via the [Nitro caching layer](https://nitro.build/guide/cache) (meaning `useRequestURL` will return `localhost` for the `host`).
|
|
14
|
+
When utilizing [Hybrid Rendering](/docs/4.x/guide/concepts/rendering#hybrid-rendering) with cache strategies, all incoming request headers are dropped when handling the cached responses via the [Nitro caching layer](https://nitro.build/guide/cache) (meaning `useRequestURL` will return `localhost` for the `host`).
|
|
15
15
|
|
|
16
16
|
You can define the [`cache.varies` option](https://nitro.build/guide/cache#options) to specify headers that will be considered when caching and serving the responses, such as `host` and `x-forwarded-host` for multi-tenant environments.
|
|
17
17
|
::
|
|
@@ -12,12 +12,12 @@ links:
|
|
|
12
12
|
This composable is available in Nuxt v3.14+.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
|
-
You can use the built-in [`useResponseHeader`](/docs/api/composables/use-response-header) composable to set any server response header within your pages, components, and plugins.
|
|
15
|
+
You can use the built-in [`useResponseHeader`](/docs/4.x/api/composables/use-response-header) composable to set any server response header within your pages, components, and plugins.
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
// Set a custom response header
|
|
19
|
-
const header = useResponseHeader('X-My-Header')
|
|
20
|
-
header.value = 'my-value'
|
|
19
|
+
const header = useResponseHeader('X-My-Header')
|
|
20
|
+
header.value = 'my-value'
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Example
|
|
@@ -27,8 +27,8 @@ We can use `useResponseHeader` to easily set a response header on a per-page bas
|
|
|
27
27
|
```vue [app/pages/test.vue]
|
|
28
28
|
<script setup>
|
|
29
29
|
// pages/test.vue
|
|
30
|
-
const header = useResponseHeader('X-My-Header')
|
|
31
|
-
header.value = 'my-value'
|
|
30
|
+
const header = useResponseHeader('X-My-Header')
|
|
31
|
+
header.value = 'my-value'
|
|
32
32
|
</script>
|
|
33
33
|
|
|
34
34
|
<template>
|
|
@@ -37,12 +37,11 @@ header.value = 'my-value';
|
|
|
37
37
|
</template>
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
We can use `useResponseHeader` for example in Nuxt [middleware](/docs/guide/directory-structure/app/middleware) to set a response header for all pages.
|
|
40
|
+
We can use `useResponseHeader` for example in Nuxt [middleware](/docs/4.x/guide/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) => {
|
|
44
|
-
const header = useResponseHeader('X-My-Always-Header')
|
|
45
|
-
header.value = `I'm Always here
|
|
46
|
-
})
|
|
47
|
-
|
|
44
|
+
const header = useResponseHeader('X-My-Always-Header')
|
|
45
|
+
header.value = `I'm Always here!`
|
|
46
|
+
})
|
|
48
47
|
```
|
|
@@ -16,7 +16,7 @@ This composable is available in Nuxt v3.12+.
|
|
|
16
16
|
|
|
17
17
|
## Description
|
|
18
18
|
|
|
19
|
-
A composable which observes the page title changes and updates the announcer message accordingly. Used by [`<NuxtRouteAnnouncer>`](/docs/api/components/nuxt-route-announcer) and controllable.
|
|
19
|
+
A composable which observes the page title changes and updates the announcer message accordingly. Used by [`<NuxtRouteAnnouncer>`](/docs/4.x/api/components/nuxt-route-announcer) and controllable.
|
|
20
20
|
It hooks into Unhead's [`dom:rendered`](https://unhead.unjs.io/docs/typescript/head/api/hooks/dom-rendered) to read the page's title and set it as the announcer message.
|
|
21
21
|
|
|
22
22
|
## Parameters
|
|
@@ -53,8 +53,8 @@ Sets the message with `politeness = "assertive"`
|
|
|
53
53
|
|
|
54
54
|
```vue [app/pages/index.vue]
|
|
55
55
|
<script setup lang="ts">
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const { message, politeness, set, polite, assertive } = useRouteAnnouncer({
|
|
57
|
+
politeness: 'assertive',
|
|
58
|
+
})
|
|
59
59
|
</script>
|
|
60
60
|
```
|
|
@@ -20,7 +20,7 @@ that rely on the route metadata, for example.
|
|
|
20
20
|
|
|
21
21
|
## Example
|
|
22
22
|
|
|
23
|
-
In the following example, we call an API via [`useFetch`](/docs/api/composables/use-fetch) using a dynamic page parameter - `slug` - as part of the URL.
|
|
23
|
+
In the following example, we call an API via [`useFetch`](/docs/4.x/api/composables/use-fetch) using a dynamic page parameter - `slug` - as part of the URL.
|
|
24
24
|
|
|
25
25
|
```html [~/pages/[slug\\].vue]
|
|
26
26
|
<script setup lang="ts">
|
|
@@ -18,7 +18,9 @@ If you only need the router instance within your template, use `$router`:
|
|
|
18
18
|
|
|
19
19
|
```vue [app/pages/index.vue]
|
|
20
20
|
<template>
|
|
21
|
-
<button @click="$router.back()">
|
|
21
|
+
<button @click="$router.back()">
|
|
22
|
+
Back
|
|
23
|
+
</button>
|
|
22
24
|
</template>
|
|
23
25
|
```
|
|
24
26
|
|
|
@@ -47,7 +49,7 @@ router.resolve({ name: 'home' })
|
|
|
47
49
|
```
|
|
48
50
|
|
|
49
51
|
::note
|
|
50
|
-
`router.addRoute()` adds route details into an array of routes and it is useful while building [Nuxt plugins](/docs/guide/directory-structure/plugins) while `router.push()` on the other hand, triggers a new navigation immediately and it is useful in pages, Vue components and composable.
|
|
52
|
+
`router.addRoute()` adds route details into an array of routes and it is useful while building [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins) while `router.push()` on the other hand, triggers a new navigation immediately and it is useful in pages, Vue components and composable.
|
|
51
53
|
::
|
|
52
54
|
|
|
53
55
|
## Based on History API
|
|
@@ -55,8 +57,8 @@ router.resolve({ name: 'home' })
|
|
|
55
57
|
- [`back()`](https://router.vuejs.org/api/interfaces/Router.html#back): Go back in history if possible, same as `router.go(-1)`.
|
|
56
58
|
- [`forward()`](https://router.vuejs.org/api/interfaces/Router.html#forward): Go forward in history if possible, same as `router.go(1)`.
|
|
57
59
|
- [`go()`](https://router.vuejs.org/api/interfaces/Router.html#go): Move forward or backward through the history without the hierarchical restrictions enforced in `router.back()` and `router.forward()`.
|
|
58
|
-
- [`push()`](https://router.vuejs.org/api/interfaces/Router.html#push): Programmatically navigate to a new URL by pushing an entry in the history stack. **It is recommended to use [`navigateTo`](/docs/api/utils/navigate-to) instead.**
|
|
59
|
-
- [`replace()`](https://router.vuejs.org/api/interfaces/Router.html#replace): Programmatically navigate to a new URL by replacing the current entry in the routes history stack. **It is recommended to use [`navigateTo`](/docs/api/utils/navigate-to) instead.**
|
|
60
|
+
- [`push()`](https://router.vuejs.org/api/interfaces/Router.html#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.html#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.**
|
|
60
62
|
|
|
61
63
|
```ts [Example]
|
|
62
64
|
const router = useRouter()
|
|
@@ -64,8 +66,8 @@ const router = useRouter()
|
|
|
64
66
|
router.back()
|
|
65
67
|
router.forward()
|
|
66
68
|
router.go(3)
|
|
67
|
-
router.push({ path:
|
|
68
|
-
router.replace({ hash:
|
|
69
|
+
router.push({ path: '/home' })
|
|
70
|
+
router.replace({ hash: '#bio' })
|
|
69
71
|
```
|
|
70
72
|
|
|
71
73
|
::read-more{icon="i-simple-icons-mdnwebdocs" to="https://developer.mozilla.org/en-US/docs/Web/API/History" target="_blank"}
|
|
@@ -78,7 +80,7 @@ Read more about the browser's History API.
|
|
|
78
80
|
|
|
79
81
|
However, Nuxt has a concept of **route middleware** that simplifies the implementation of navigation guards and provides a better developer experience.
|
|
80
82
|
|
|
81
|
-
:read-more{to="/docs/guide/directory-structure/app/middleware"}
|
|
83
|
+
:read-more{to="/docs/4.x/guide/directory-structure/app/middleware"}
|
|
82
84
|
|
|
83
85
|
## Promise and Error Handling
|
|
84
86
|
|
|
@@ -89,4 +91,4 @@ However, Nuxt has a concept of **route middleware** that simplifies the implemen
|
|
|
89
91
|
|
|
90
92
|
## Universal Router Instance
|
|
91
93
|
|
|
92
|
-
If you do not have a `app/pages/` folder, then [`useRouter`](/docs/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`.
|
|
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`.
|