@nuxt/docs 0.0.0 → 3.17.0
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/.navigation.yml +2 -0
- package/1.getting-started/.navigation.yml +3 -0
- package/1.getting-started/01.introduction.md +81 -0
- package/1.getting-started/02.installation.md +109 -0
- package/1.getting-started/03.configuration.md +226 -0
- package/1.getting-started/04.views.md +163 -0
- package/1.getting-started/05.assets.md +48 -0
- package/1.getting-started/06.styling.md +565 -0
- package/1.getting-started/07.routing.md +149 -0
- package/1.getting-started/08.seo-meta.md +360 -0
- package/1.getting-started/09.transitions.md +473 -0
- package/1.getting-started/10.data-fetching.md +795 -0
- package/1.getting-started/11.state-management.md +223 -0
- package/1.getting-started/12.error-handling.md +233 -0
- package/1.getting-started/13.server.md +94 -0
- package/1.getting-started/14.layers.md +92 -0
- package/1.getting-started/15.prerendering.md +194 -0
- package/1.getting-started/16.deployment.md +130 -0
- package/1.getting-started/17.testing.md +728 -0
- package/1.getting-started/18.upgrade.md +997 -0
- package/2.guide/.navigation.yml +2 -0
- package/2.guide/0.index.md +22 -0
- package/2.guide/1.concepts/.navigation.yml +3 -0
- package/2.guide/1.concepts/1.auto-imports.md +205 -0
- package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
- package/2.guide/1.concepts/2.vuejs-development.md +103 -0
- package/2.guide/1.concepts/3.rendering.md +255 -0
- package/2.guide/1.concepts/4.server-engine.md +62 -0
- package/2.guide/1.concepts/5.modules.md +48 -0
- package/2.guide/1.concepts/7.esm.md +299 -0
- package/2.guide/1.concepts/8.typescript.md +97 -0
- package/2.guide/1.concepts/9.code-style.md +22 -0
- package/2.guide/2.directory-structure/.navigation.yml +3 -0
- package/2.guide/2.directory-structure/0.nuxt.md +20 -0
- package/2.guide/2.directory-structure/0.output.md +18 -0
- package/2.guide/2.directory-structure/1.assets.md +16 -0
- package/2.guide/2.directory-structure/1.components.md +608 -0
- package/2.guide/2.directory-structure/1.composables.md +121 -0
- package/2.guide/2.directory-structure/1.content.md +64 -0
- package/2.guide/2.directory-structure/1.layouts.md +180 -0
- package/2.guide/2.directory-structure/1.middleware.md +209 -0
- package/2.guide/2.directory-structure/1.modules.md +66 -0
- package/2.guide/2.directory-structure/1.node_modules.md +12 -0
- package/2.guide/2.directory-structure/1.pages.md +440 -0
- package/2.guide/2.directory-structure/1.plugins.md +299 -0
- package/2.guide/2.directory-structure/1.public.md +27 -0
- package/2.guide/2.directory-structure/1.server.md +546 -0
- package/2.guide/2.directory-structure/1.shared.md +104 -0
- package/2.guide/2.directory-structure/1.utils.md +49 -0
- package/2.guide/2.directory-structure/2.env.md +75 -0
- package/2.guide/2.directory-structure/2.gitignore.md +37 -0
- package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
- package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
- package/2.guide/2.directory-structure/3.app-config.md +177 -0
- package/2.guide/2.directory-structure/3.app.md +72 -0
- package/2.guide/2.directory-structure/3.error.md +55 -0
- package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
- package/2.guide/2.directory-structure/3.package.md +32 -0
- package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
- package/2.guide/3.going-further/.navigation.yml +3 -0
- package/2.guide/3.going-further/1.experimental-features.md +689 -0
- package/2.guide/3.going-further/1.features.md +103 -0
- package/2.guide/3.going-further/1.internals.md +81 -0
- package/2.guide/3.going-further/10.runtime-config.md +174 -0
- package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
- package/2.guide/3.going-further/2.hooks.md +98 -0
- package/2.guide/3.going-further/3.modules.md +811 -0
- package/2.guide/3.going-further/4.kit.md +51 -0
- package/2.guide/3.going-further/6.nuxt-app.md +64 -0
- package/2.guide/3.going-further/7.layers.md +227 -0
- package/2.guide/3.going-further/9.debugging.md +115 -0
- package/2.guide/3.going-further/index.md +4 -0
- package/2.guide/4.recipes/.navigation.yml +3 -0
- package/2.guide/4.recipes/1.custom-routing.md +181 -0
- package/2.guide/4.recipes/2.vite-plugin.md +65 -0
- package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
- package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
- package/3.api/.navigation.yml +3 -0
- package/3.api/1.components/.navigation.yml +3 -0
- package/3.api/1.components/1.client-only.md +76 -0
- package/3.api/1.components/1.dev-only.md +51 -0
- package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
- package/3.api/1.components/10.nuxt-picture.md +27 -0
- package/3.api/1.components/11.teleports.md +40 -0
- package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
- package/3.api/1.components/13.nuxt-time.md +173 -0
- package/3.api/1.components/2.nuxt-page.md +154 -0
- package/3.api/1.components/3.nuxt-layout.md +156 -0
- package/3.api/1.components/4.nuxt-link.md +322 -0
- package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
- package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
- package/3.api/1.components/7.nuxt-welcome.md +25 -0
- package/3.api/1.components/8.nuxt-island.md +70 -0
- package/3.api/1.components/9.nuxt-img.md +43 -0
- package/3.api/2.composables/.navigation.yml +3 -0
- package/3.api/2.composables/on-prehydrate.md +60 -0
- package/3.api/2.composables/use-app-config.md +19 -0
- package/3.api/2.composables/use-async-data.md +212 -0
- package/3.api/2.composables/use-cookie.md +233 -0
- package/3.api/2.composables/use-error.md +32 -0
- package/3.api/2.composables/use-fetch.md +217 -0
- package/3.api/2.composables/use-head-safe.md +55 -0
- package/3.api/2.composables/use-head.md +69 -0
- package/3.api/2.composables/use-hydration.md +68 -0
- package/3.api/2.composables/use-lazy-async-data.md +47 -0
- package/3.api/2.composables/use-lazy-fetch.md +55 -0
- package/3.api/2.composables/use-loading-indicator.md +77 -0
- package/3.api/2.composables/use-nuxt-app.md +294 -0
- package/3.api/2.composables/use-nuxt-data.md +112 -0
- package/3.api/2.composables/use-preview-mode.md +118 -0
- package/3.api/2.composables/use-request-event.md +23 -0
- package/3.api/2.composables/use-request-fetch.md +52 -0
- package/3.api/2.composables/use-request-header.md +34 -0
- package/3.api/2.composables/use-request-headers.md +37 -0
- package/3.api/2.composables/use-request-url.md +41 -0
- package/3.api/2.composables/use-response-header.md +48 -0
- package/3.api/2.composables/use-route-announcer.md +60 -0
- package/3.api/2.composables/use-route.md +52 -0
- package/3.api/2.composables/use-router.md +92 -0
- package/3.api/2.composables/use-runtime-config.md +142 -0
- package/3.api/2.composables/use-runtime-hook.md +43 -0
- package/3.api/2.composables/use-seo-meta.md +80 -0
- package/3.api/2.composables/use-server-seo-meta.md +27 -0
- package/3.api/2.composables/use-state.md +48 -0
- package/3.api/3.utils/$fetch.md +98 -0
- package/3.api/3.utils/.navigation.yml +3 -0
- package/3.api/3.utils/abort-navigation.md +73 -0
- package/3.api/3.utils/add-route-middleware.md +88 -0
- package/3.api/3.utils/call-once.md +92 -0
- package/3.api/3.utils/clear-error.md +29 -0
- package/3.api/3.utils/clear-nuxt-data.md +23 -0
- package/3.api/3.utils/clear-nuxt-state.md +23 -0
- package/3.api/3.utils/create-error.md +55 -0
- package/3.api/3.utils/define-nuxt-component.md +53 -0
- package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
- package/3.api/3.utils/define-page-meta.md +234 -0
- package/3.api/3.utils/define-route-rules.md +52 -0
- package/3.api/3.utils/navigate-to.md +230 -0
- package/3.api/3.utils/on-before-route-leave.md +11 -0
- package/3.api/3.utils/on-before-route-update.md +11 -0
- package/3.api/3.utils/on-nuxt-ready.md +25 -0
- package/3.api/3.utils/prefetch-components.md +28 -0
- package/3.api/3.utils/preload-components.md +23 -0
- package/3.api/3.utils/preload-route-components.md +41 -0
- package/3.api/3.utils/prerender-routes.md +46 -0
- package/3.api/3.utils/refresh-cookie.md +46 -0
- package/3.api/3.utils/refresh-nuxt-data.md +91 -0
- package/3.api/3.utils/reload-nuxt-app.md +74 -0
- package/3.api/3.utils/set-page-layout.md +24 -0
- package/3.api/3.utils/set-response-status.md +36 -0
- package/3.api/3.utils/show-error.md +31 -0
- package/3.api/3.utils/update-app-config.md +27 -0
- package/3.api/4.commands/.navigation.yml +3 -0
- package/3.api/4.commands/add.md +112 -0
- package/3.api/4.commands/analyze.md +41 -0
- package/3.api/4.commands/build-module.md +42 -0
- package/3.api/4.commands/build.md +46 -0
- package/3.api/4.commands/cleanup.md +38 -0
- package/3.api/4.commands/dev.md +59 -0
- package/3.api/4.commands/devtools.md +38 -0
- package/3.api/4.commands/generate.md +41 -0
- package/3.api/4.commands/info.md +33 -0
- package/3.api/4.commands/init.md +46 -0
- package/3.api/4.commands/module.md +84 -0
- package/3.api/4.commands/prepare.md +36 -0
- package/3.api/4.commands/preview.md +43 -0
- package/3.api/4.commands/typecheck.md +42 -0
- package/3.api/4.commands/upgrade.md +37 -0
- package/3.api/5.kit/.navigation.yml +3 -0
- package/3.api/5.kit/1.modules.md +172 -0
- package/3.api/5.kit/10.runtime-config.md +27 -0
- package/3.api/5.kit/10.templates.md +283 -0
- package/3.api/5.kit/11.nitro.md +409 -0
- package/3.api/5.kit/12.resolving.md +268 -0
- package/3.api/5.kit/13.logging.md +65 -0
- package/3.api/5.kit/14.builder.md +491 -0
- package/3.api/5.kit/15.examples.md +41 -0
- package/3.api/5.kit/2.programmatic.md +125 -0
- package/3.api/5.kit/3.compatibility.md +230 -0
- package/3.api/5.kit/4.autoimports.md +144 -0
- package/3.api/5.kit/5.components.md +127 -0
- package/3.api/5.kit/6.context.md +130 -0
- package/3.api/5.kit/7.pages.md +295 -0
- package/3.api/5.kit/8.layout.md +80 -0
- package/3.api/5.kit/9.plugins.md +263 -0
- package/3.api/6.advanced/.navigation.yml +1 -0
- package/3.api/6.advanced/1.hooks.md +105 -0
- package/3.api/6.advanced/2.import-meta.md +60 -0
- package/3.api/6.nuxt-config.md +12 -0
- package/3.api/index.md +31 -0
- package/5.community/.navigation.yml +3 -0
- package/5.community/2.getting-help.md +48 -0
- package/5.community/3.reporting-bugs.md +50 -0
- package/5.community/4.contribution.md +205 -0
- package/5.community/5.framework-contribution.md +142 -0
- package/5.community/6.roadmap.md +79 -0
- package/5.community/7.changelog.md +92 -0
- package/6.bridge/.navigation.yml +3 -0
- package/6.bridge/1.overview.md +137 -0
- package/6.bridge/10.configuration.md +96 -0
- package/6.bridge/2.typescript.md +46 -0
- package/6.bridge/3.bridge-composition-api.md +132 -0
- package/6.bridge/4.plugins-and-middleware.md +65 -0
- package/6.bridge/5.nuxt3-compatible-api.md +204 -0
- package/6.bridge/6.meta.md +117 -0
- package/6.bridge/7.runtime-config.md +38 -0
- package/6.bridge/8.nitro.md +102 -0
- package/6.bridge/9.vite.md +37 -0
- package/7.migration/.navigation.yml +3 -0
- package/7.migration/1.overview.md +24 -0
- package/7.migration/10.bundling.md +28 -0
- package/7.migration/11.server.md +17 -0
- package/7.migration/2.configuration.md +240 -0
- package/7.migration/20.module-authors.md +94 -0
- package/7.migration/3.auto-imports.md +18 -0
- package/7.migration/4.meta.md +127 -0
- package/7.migration/5.plugins-and-middleware.md +80 -0
- package/7.migration/6.pages-and-layouts.md +233 -0
- package/7.migration/7.component-options.md +156 -0
- package/7.migration/8.runtime-config.md +58 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/package.json +16 -4
- package/dist/.gitkeep +0 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Pages and Layouts
|
|
3
|
+
description: Learn how to migrate from Nuxt 2 to Nuxt 3 pages and layouts.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## `app.vue`
|
|
7
|
+
|
|
8
|
+
Nuxt 3 provides a central entry point to your app via `~/app.vue`.
|
|
9
|
+
|
|
10
|
+
::note
|
|
11
|
+
If you don't have an `app.vue` file in your source directory, Nuxt will use its own default version.
|
|
12
|
+
::
|
|
13
|
+
|
|
14
|
+
This file is a great place to put any custom code that needs to be run once when your app starts up, as well as any components that are present on every page of your app. For example, if you only have one layout, you can move this to `app.vue` instead.
|
|
15
|
+
|
|
16
|
+
:read-more{to="/docs/guide/directory-structure/app"}
|
|
17
|
+
|
|
18
|
+
:link-example{to="/docs/examples/hello-world"}
|
|
19
|
+
|
|
20
|
+
### Migration
|
|
21
|
+
|
|
22
|
+
Consider creating an `app.vue` file and including any logic that needs to run once at the top-level of your app. You can check out [an example here](/docs/guide/directory-structure/app).
|
|
23
|
+
|
|
24
|
+
## Layouts
|
|
25
|
+
|
|
26
|
+
If you are using layouts in your app for multiple pages, there is only a slight change required.
|
|
27
|
+
|
|
28
|
+
In Nuxt 2, the `<Nuxt>` component is used within a layout to render the current page. In Nuxt 3, layouts use slots instead, so you will have to replace that component with a `<slot />`. This also allows advanced use cases with named and scoped slots. [Read more about layouts](/docs/guide/directory-structure/layouts).
|
|
29
|
+
|
|
30
|
+
You will also need to change how you define the layout used by a page using the `definePageMeta` compiler macro. Layouts will be kebab-cased. So `layouts/customLayout.vue` becomes `custom-layout` when referenced in your page.
|
|
31
|
+
|
|
32
|
+
### Migration
|
|
33
|
+
|
|
34
|
+
1. Replace `<Nuxt />` with `<slot />`
|
|
35
|
+
```diff [layouts/custom.vue]
|
|
36
|
+
<template>
|
|
37
|
+
<div id="app-layout">
|
|
38
|
+
<main>
|
|
39
|
+
- <Nuxt />
|
|
40
|
+
+ <slot />
|
|
41
|
+
</main>
|
|
42
|
+
</div>
|
|
43
|
+
</template>
|
|
44
|
+
```
|
|
45
|
+
2. Use [`definePageMeta`](/docs/api/utils/define-page-meta) to select the layout used by your page.
|
|
46
|
+
```diff [pages/index.vue]
|
|
47
|
+
+ <script setup>
|
|
48
|
+
+ definePageMeta({
|
|
49
|
+
+ layout: 'custom'
|
|
50
|
+
+ })
|
|
51
|
+
- <script>
|
|
52
|
+
- export default {
|
|
53
|
+
- layout: 'custom'
|
|
54
|
+
- }
|
|
55
|
+
</script>
|
|
56
|
+
```
|
|
57
|
+
3. Move `~/layouts/_error.vue` to `~/error.vue`. See [the error handling docs](/docs/getting-started/error-handling). If you want to ensure that this page uses a layout, you can use [`<NuxtLayout>`](/docs/guide/directory-structure/layouts) directly within `error.vue`:
|
|
58
|
+
```vue [error.vue]
|
|
59
|
+
<template>
|
|
60
|
+
<div>
|
|
61
|
+
<NuxtLayout name="default">
|
|
62
|
+
<!-- -->
|
|
63
|
+
</NuxtLayout>
|
|
64
|
+
</div>
|
|
65
|
+
</template>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Pages
|
|
69
|
+
|
|
70
|
+
Nuxt 3 ships with an optional `vue-router` integration triggered by the existence of a [`pages/`](/docs/guide/directory-structure/pages) directory in your source directory. If you only have a single page, you may consider instead moving it to `app.vue` for a lighter build.
|
|
71
|
+
|
|
72
|
+
### Dynamic Routes
|
|
73
|
+
|
|
74
|
+
The format for defining dynamic routes in Nuxt 3 is slightly different from Nuxt 2, so you may need to rename some of the files within `pages/`.
|
|
75
|
+
|
|
76
|
+
1. Where you previously used `_id` to define a dynamic route parameter you now use `[id]`.
|
|
77
|
+
2. Where you previously used `_.vue` to define a catch-all route, you now use `[...slug].vue`.
|
|
78
|
+
|
|
79
|
+
### Nested Routes
|
|
80
|
+
|
|
81
|
+
In Nuxt 2, you will have defined any nested routes (with parent and child components) using `<Nuxt>` and `<NuxtChild>`. In Nuxt 3, these have been replaced with a single `<NuxtPage>` component.
|
|
82
|
+
|
|
83
|
+
### Page Keys and Keep-alive Props
|
|
84
|
+
|
|
85
|
+
If you were passing a custom page key or keep-alive props to `<Nuxt>`, you will now use `definePageMeta` to set these options.
|
|
86
|
+
|
|
87
|
+
:read-more{to="/docs/guide/directory-structure/pages#special-metadata"}
|
|
88
|
+
|
|
89
|
+
### Page and Layout Transitions
|
|
90
|
+
|
|
91
|
+
If you have been defining transitions for your page or layout directly in your component options, you will now need to use `definePageMeta` to set the transition. Since Vue 3, [-enter and -leave CSS classes have been renamed](https://v3-migration.vuejs.org/breaking-changes/transition.html). The `style` prop from `<Nuxt>` no longer applies to transition when used on `<slot>`, so move the styles to your `-active` class.
|
|
92
|
+
|
|
93
|
+
:read-more{to="/docs/getting-started/transitions"}
|
|
94
|
+
|
|
95
|
+
### Migration
|
|
96
|
+
|
|
97
|
+
1. Rename any pages with dynamic parameters to match the new format.
|
|
98
|
+
2. Update `<Nuxt>` and `<NuxtChild>` to be `<NuxtPage>`.
|
|
99
|
+
3. If you're using the Composition API, you can also migrate `this.$route` and `this.$router` to use [`useRoute`](/docs/api/composables/use-route) and [`useRouter`](/docs/api/composables/use-router) composables.
|
|
100
|
+
|
|
101
|
+
#### Example: Dynamic Routes
|
|
102
|
+
|
|
103
|
+
::code-group
|
|
104
|
+
|
|
105
|
+
``` [Nuxt 2]
|
|
106
|
+
- URL: /users
|
|
107
|
+
- Page: /pages/users/index.vue
|
|
108
|
+
|
|
109
|
+
- URL: /users/some-user-name
|
|
110
|
+
- Page: /pages/users/_user.vue
|
|
111
|
+
- Usage: params.user
|
|
112
|
+
|
|
113
|
+
- URL: /users/some-user-name/edit
|
|
114
|
+
- Page: /pages/users/_user/edit.vue
|
|
115
|
+
- Usage: params.user
|
|
116
|
+
|
|
117
|
+
- URL: /users/anything-else
|
|
118
|
+
- Page: /pages/users/_.vue
|
|
119
|
+
- Usage: params.pathMatch
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
``` [Nuxt 3]
|
|
123
|
+
- URL: /users
|
|
124
|
+
- Page: /pages/users/index.vue
|
|
125
|
+
|
|
126
|
+
- URL: /users/some-user-name
|
|
127
|
+
- Page: /pages/users/[user].vue
|
|
128
|
+
- Usage: params.user
|
|
129
|
+
|
|
130
|
+
- URL: /users/some-user-name/edit
|
|
131
|
+
- Page: /pages/users/[user]/edit.vue
|
|
132
|
+
- Usage: params.user
|
|
133
|
+
|
|
134
|
+
- URL: /users/anything-else
|
|
135
|
+
- Page: /pages/users/[...slug].vue
|
|
136
|
+
- Usage: params.slug
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
::
|
|
140
|
+
|
|
141
|
+
#### Example: Nested Routes and `definePageMeta`
|
|
142
|
+
|
|
143
|
+
::code-group
|
|
144
|
+
|
|
145
|
+
```vue [Nuxt 2]
|
|
146
|
+
<template>
|
|
147
|
+
<div>
|
|
148
|
+
<NuxtChild keep-alive :keep-alive-props="{ exclude: ['modal'] }" :nuxt-child-key="$route.slug" />
|
|
149
|
+
</div>
|
|
150
|
+
</template>
|
|
151
|
+
|
|
152
|
+
<script>
|
|
153
|
+
export default {
|
|
154
|
+
transition: 'page' // or { name: 'page' }
|
|
155
|
+
}
|
|
156
|
+
</script>
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```vue [Nuxt 3]
|
|
160
|
+
<template>
|
|
161
|
+
<div>
|
|
162
|
+
<NuxtPage />
|
|
163
|
+
</div>
|
|
164
|
+
</template>
|
|
165
|
+
|
|
166
|
+
<script setup lang="ts">
|
|
167
|
+
// This compiler macro works in both <script> and <script setup>
|
|
168
|
+
definePageMeta({
|
|
169
|
+
// you can also pass a string or a computed property
|
|
170
|
+
key: route => route.slug,
|
|
171
|
+
transition: {
|
|
172
|
+
name: 'page',
|
|
173
|
+
},
|
|
174
|
+
keepalive: {
|
|
175
|
+
exclude: ['modal']
|
|
176
|
+
},
|
|
177
|
+
})
|
|
178
|
+
</script>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
::
|
|
182
|
+
|
|
183
|
+
## `<NuxtLink>` Component
|
|
184
|
+
|
|
185
|
+
Most of the syntax and functionality are the same for the global [NuxtLink](/docs/api/components/nuxt-link) component. If you have been using the shortcut `<NLink>` format, you should update this to use `<NuxtLink>`.
|
|
186
|
+
|
|
187
|
+
`<NuxtLink>` is now a drop-in replacement for _all_ links, even external ones. You can read more about it, and how to extend it to provide your own link component.
|
|
188
|
+
|
|
189
|
+
:read-more{to="/docs/api/components/nuxt-link"}
|
|
190
|
+
|
|
191
|
+
## Programmatic Navigation
|
|
192
|
+
|
|
193
|
+
When migrating from Nuxt 2 to Nuxt 3, you will have to update how you programmatically navigate your users. In Nuxt 2, you had access to the underlying Vue Router with `this.$router`. In Nuxt 3, you can use the `navigateTo()` utility method which allows you to pass a route and parameters to Vue Router.
|
|
194
|
+
|
|
195
|
+
::warning
|
|
196
|
+
Ensure to always `await` on [`navigateTo`](/docs/api/utils/navigate-to) or chain its result by returning from functions.
|
|
197
|
+
::
|
|
198
|
+
|
|
199
|
+
::code-group
|
|
200
|
+
|
|
201
|
+
```vue [Nuxt 2]
|
|
202
|
+
<script>
|
|
203
|
+
export default {
|
|
204
|
+
methods: {
|
|
205
|
+
navigate(){
|
|
206
|
+
this.$router.push({
|
|
207
|
+
path: '/search',
|
|
208
|
+
query: {
|
|
209
|
+
name: 'first name',
|
|
210
|
+
type: '1'
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
</script>
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
```vue [Nuxt 3]
|
|
220
|
+
<script setup lang="ts">
|
|
221
|
+
function navigate(){
|
|
222
|
+
return navigateTo({
|
|
223
|
+
path: '/search',
|
|
224
|
+
query: {
|
|
225
|
+
name: 'first name',
|
|
226
|
+
type: '1'
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
</script>
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
::
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Component Options
|
|
3
|
+
description: 'Learn how to migrate from Nuxt 2 components options to Nuxt 3 composables.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## `asyncData` and `fetch`
|
|
7
|
+
|
|
8
|
+
Nuxt 3 provides new options for [fetching data from an API](/docs/getting-started/data-fetching).
|
|
9
|
+
|
|
10
|
+
<!-- TODO: Intro to <script setup> -->
|
|
11
|
+
<!-- TODO: Mention about options compatibility with asyncData -->
|
|
12
|
+
|
|
13
|
+
### Isomorphic Fetch
|
|
14
|
+
|
|
15
|
+
In Nuxt 2 you might use `@nuxtjs/axios` or `@nuxt/http` to fetch your data - or just the polyfilled global `fetch`.
|
|
16
|
+
|
|
17
|
+
In Nuxt 3 you can use a globally available `fetch` method that has the same API as [the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) or [`$fetch`](/docs/api/utils/dollarfetch) method which is using [unjs/ofetch](https://github.com/unjs/ofetch). It has a number of benefits, including:
|
|
18
|
+
|
|
19
|
+
1. It will handle 'smartly' making [direct API calls](/docs/guide/concepts/server-engine#direct-api-calls) if it's running on the server, or making a client-side call to your API if it's running on the client. (It can also handle calling third-party APIs.)
|
|
20
|
+
|
|
21
|
+
2. Plus, it comes with convenience features including automatically parsing responses and stringifying data.
|
|
22
|
+
|
|
23
|
+
You can read more [about direct API calls](/docs/guide/concepts/server-engine#direct-api-calls) or [fetching data](/docs/getting-started/data-fetching).
|
|
24
|
+
|
|
25
|
+
### Composables
|
|
26
|
+
|
|
27
|
+
Nuxt 3 provides new composables for fetching data: [`useAsyncData`](/docs/api/composables/use-async-data) and `useFetch`. They each have 'lazy' variants (`useLazyAsyncData` and `useLazyFetch`), which do not block client-side navigation.
|
|
28
|
+
|
|
29
|
+
In Nuxt 2, you'd fetch your data in your component using a syntax similar to:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
export default {
|
|
33
|
+
async asyncData({ params, $http }) {
|
|
34
|
+
const post = await $http.$get(`https://api.nuxtjs.dev/posts/${params.id}`)
|
|
35
|
+
return { post }
|
|
36
|
+
},
|
|
37
|
+
// or alternatively
|
|
38
|
+
fetch () {
|
|
39
|
+
this.post = await $http.$get(`https://api.nuxtjs.dev/posts/${params.id}`)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Within your methods and templates, you could use the `post` variable similar how you'd use any other piece of data provided by your component.
|
|
45
|
+
|
|
46
|
+
With Nuxt 3, you can perform this data fetching using composables in your `setup()` method or `<script setup>` tag:
|
|
47
|
+
|
|
48
|
+
```vue
|
|
49
|
+
<script setup lang="ts">
|
|
50
|
+
// Define params wherever, through `defineProps()`, `useRoute()`, etc.
|
|
51
|
+
const { data: post, refresh } = await useAsyncData('post', () => $fetch(`https://api.nuxtjs.dev/posts/${params.id}`) )
|
|
52
|
+
// Or instead - useFetch is a convenience wrapper around useAsyncData when you're just performing a simple fetch
|
|
53
|
+
const { data: post, refresh } = await useFetch(`https://api.nuxtjs.dev/posts/${params.id}`)
|
|
54
|
+
</script>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can now use `post` inside of your Nuxt 3 template, or call `refresh` to update the data.
|
|
58
|
+
|
|
59
|
+
::note
|
|
60
|
+
Despite the names, [`useFetch`](/docs/api/composables/use-fetch) is not a direct replacement of the `fetch()` hook. Rather, [`useAsyncData`](/docs/api/composables/use-async-data) replaces both hooks and is more customizable; it can do more than simply fetching data from an endpoint. [`useFetch`](/docs/api/composables/use-fetch) is a convenience wrapper around [`useAsyncData`](/docs/api/composables/use-async-data) for simply fetching data from an endpoint.
|
|
61
|
+
::
|
|
62
|
+
|
|
63
|
+
### Migration
|
|
64
|
+
|
|
65
|
+
1. Replace the `asyncData` hook with [`useAsyncData`](/docs/api/composables/use-async-data) or [`useFetch`](/docs/api/composables/use-fetch) in your page/component.
|
|
66
|
+
2. Replace the `fetch` hook with [`useAsyncData`](/docs/api/composables/use-async-data) or [`useFetch`](/docs/api/composables/use-fetch) in your component.
|
|
67
|
+
|
|
68
|
+
## `head`
|
|
69
|
+
|
|
70
|
+
:read-more{to="/docs/migration/meta"}
|
|
71
|
+
|
|
72
|
+
## `key`
|
|
73
|
+
|
|
74
|
+
You can now define a key within the [`definePageMeta`](/docs/api/utils/define-page-meta) compiler macro.
|
|
75
|
+
|
|
76
|
+
```diff [pages/index.vue]
|
|
77
|
+
- <script>
|
|
78
|
+
- export default {
|
|
79
|
+
- key: 'index'
|
|
80
|
+
- // or a method
|
|
81
|
+
- // key: route => route.fullPath
|
|
82
|
+
- }
|
|
83
|
+
+ <script setup>
|
|
84
|
+
+ definePageMeta({
|
|
85
|
+
+ key: 'index'
|
|
86
|
+
+ // or a method
|
|
87
|
+
+ // key: route => route.fullPath
|
|
88
|
+
+ })
|
|
89
|
+
</script>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## `layout`
|
|
93
|
+
|
|
94
|
+
:read-more{to="/docs/migration/pages-and-layouts"}
|
|
95
|
+
|
|
96
|
+
## `loading`
|
|
97
|
+
|
|
98
|
+
This feature is not yet supported in Nuxt 3.
|
|
99
|
+
|
|
100
|
+
## `middleware`
|
|
101
|
+
|
|
102
|
+
:read-more{to="/docs/migration/plugins-and-middleware"}
|
|
103
|
+
|
|
104
|
+
## `scrollToTop`
|
|
105
|
+
|
|
106
|
+
This feature is not yet supported in Nuxt 3. If you want to overwrite the default scroll behavior of `vue-router`, you can do so in `~/app/router.options.ts` (see [docs](/docs/guide/recipes/custom-routing#router-options)) for more info.
|
|
107
|
+
Similar to `key`, specify it within the [`definePageMeta`](/docs/api/utils/define-page-meta) compiler macro.
|
|
108
|
+
|
|
109
|
+
```diff [pages/index.vue]
|
|
110
|
+
- <script>
|
|
111
|
+
- export default {
|
|
112
|
+
- scrollToTop: false
|
|
113
|
+
- }
|
|
114
|
+
+ <script setup>
|
|
115
|
+
+ definePageMeta({
|
|
116
|
+
+ scrollToTop: false
|
|
117
|
+
+ })
|
|
118
|
+
</script>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## `transition`
|
|
122
|
+
|
|
123
|
+
:read-more{to="/docs/getting-started/transitions"}
|
|
124
|
+
|
|
125
|
+
## `validate`
|
|
126
|
+
|
|
127
|
+
The validate hook in Nuxt 3 only accepts a single argument, the `route`. Just as in Nuxt 2, you can return a boolean value. If you return false and another match can't be found, this will mean a 404. You can also directly return an object with `statusCode`/`statusMessage` to respond immediately with an error (other matches will not be checked).
|
|
128
|
+
|
|
129
|
+
```diff [pages/users/[id\\].vue]
|
|
130
|
+
- <script>
|
|
131
|
+
- export default {
|
|
132
|
+
- async validate({ params }) {
|
|
133
|
+
- return /^\d+$/.test(params.id)
|
|
134
|
+
- }
|
|
135
|
+
- }
|
|
136
|
+
+ <script setup>
|
|
137
|
+
+ definePageMeta({
|
|
138
|
+
+ validate: async (route) => {
|
|
139
|
+
+ const nuxtApp = useNuxtApp()
|
|
140
|
+
+ return /^\d+$/.test(route.params.id)
|
|
141
|
+
+ }
|
|
142
|
+
+ })
|
|
143
|
+
</script>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## `watchQuery`
|
|
147
|
+
|
|
148
|
+
This is not supported in Nuxt 3. Instead, you can directly use a watcher to trigger refetching data.
|
|
149
|
+
|
|
150
|
+
```vue [pages/users/[id\\].vue]
|
|
151
|
+
<script setup lang="ts">
|
|
152
|
+
const route = useRoute()
|
|
153
|
+
const { data, refresh } = await useFetch('/api/user')
|
|
154
|
+
watch(() => route.query, () => refresh())
|
|
155
|
+
</script>
|
|
156
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Runtime Config
|
|
3
|
+
description: 'Learn how to migrate from Nuxt 2 to Nuxt 3 runtime config.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
If you wish to reference environment variables within your Nuxt 3 app, you will need to use runtime config.
|
|
7
|
+
|
|
8
|
+
When referencing these variables within your components, you will have to use the [`useRuntimeConfig`](/docs/api/composables/use-runtime-config) composable in your setup method (or Nuxt plugin).
|
|
9
|
+
|
|
10
|
+
In the `server/` portion of your app, you can use [`useRuntimeConfig`](/docs/api/composables/use-runtime-config) without any import.
|
|
11
|
+
|
|
12
|
+
:read-more{to="/docs/guide/going-further/runtime-config"}
|
|
13
|
+
|
|
14
|
+
## Migration
|
|
15
|
+
|
|
16
|
+
1. Add any environment variables that you use in your app to the `runtimeConfig` property of the `nuxt.config` file.
|
|
17
|
+
2. Migrate `process.env` to [`useRuntimeConfig`](/docs/api/composables/use-runtime-config) throughout the Vue part of your app.
|
|
18
|
+
|
|
19
|
+
::code-group
|
|
20
|
+
|
|
21
|
+
```ts [nuxt.config.ts]
|
|
22
|
+
export default defineNuxtConfig({
|
|
23
|
+
runtimeConfig: {
|
|
24
|
+
// Private config that is only available on the server
|
|
25
|
+
apiSecret: '123',
|
|
26
|
+
// Config within public will be also exposed to the client
|
|
27
|
+
public: {
|
|
28
|
+
apiBase: '/api'
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```vue [pages/index.vue]
|
|
35
|
+
<script setup lang="ts">
|
|
36
|
+
const config = useRuntimeConfig()
|
|
37
|
+
|
|
38
|
+
// instead of process.env you will now access config.public.apiBase
|
|
39
|
+
console.log(config.public.apiBase)
|
|
40
|
+
</script>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```ts [server/api/hello.ts]
|
|
44
|
+
export default defineEventhandler((event) => {
|
|
45
|
+
const config = useRuntimeConfig(event)
|
|
46
|
+
// In server, you can now access config.apiSecret, in addition to config.public
|
|
47
|
+
console.log(config.apiSecret)
|
|
48
|
+
console.log(config.public.apiBase)
|
|
49
|
+
})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```ini [.env]
|
|
53
|
+
# Runtime config values are automatically replaced by matching environment variables at runtime
|
|
54
|
+
NUXT_API_SECRET=api_secret_token
|
|
55
|
+
NUXT_PUBLIC_API_BASE=https://nuxtjs.org
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
::
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present - Nuxt Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuxt/docs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.17.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/nuxt/nuxt.git",
|
|
7
|
+
"directory": "docs"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://nuxt.com",
|
|
10
|
+
"description": "Documentation for Nuxt in raw markdown",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
"./*": "./*"
|
|
15
|
+
},
|
|
4
16
|
"files": [
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
"**/*.md",
|
|
18
|
+
"**/*.yml"
|
|
19
|
+
]
|
|
8
20
|
}
|
package/dist/.gitkeep
DELETED
|
File without changes
|