@nuxt/docs 4.1.1 → 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 +37 -37
- 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 +27 -20
- 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 +49 -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 -8
- 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 +21 -20
- 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
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
title: "components"
|
|
3
3
|
head.title: "components/"
|
|
4
4
|
description: "The components/ directory is where you put all your Vue components."
|
|
5
|
-
navigation.icon: i-
|
|
5
|
+
navigation.icon: i-vscode-icons-folder-type-component
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
Nuxt automatically imports any components in this directory (along with components that are registered by any modules you may be using).
|
|
@@ -54,7 +54,7 @@ export default defineNuxtConfig({
|
|
|
54
54
|
pathPrefix: false, // [!code ++]
|
|
55
55
|
},
|
|
56
56
|
],
|
|
57
|
-
})
|
|
57
|
+
})
|
|
58
58
|
```
|
|
59
59
|
|
|
60
60
|
This registers the components using the same strategy as used in Nuxt 2. For example, `~/components/Some/MyComponent.vue` will be usable as `<MyComponent>` and not `<SomeMyComponent>`.
|
|
@@ -116,7 +116,12 @@ const show = ref(false)
|
|
|
116
116
|
<div>
|
|
117
117
|
<h1>Mountains</h1>
|
|
118
118
|
<LazyMountainsList v-if="show" />
|
|
119
|
-
<button
|
|
119
|
+
<button
|
|
120
|
+
v-if="!show"
|
|
121
|
+
@click="show = true"
|
|
122
|
+
>
|
|
123
|
+
Show List
|
|
124
|
+
</button>
|
|
120
125
|
</div>
|
|
121
126
|
</template>
|
|
122
127
|
```
|
|
@@ -133,6 +138,10 @@ Nuxt supports this using lazy (or delayed) hydration, allowing you to control wh
|
|
|
133
138
|
|
|
134
139
|
Nuxt provides a range of built-in hydration strategies. Only one strategy can be used per lazy component.
|
|
135
140
|
|
|
141
|
+
::note
|
|
142
|
+
Any prop change on a lazily hydrated component will trigger hydration immediately. (e.g., changing a prop on a component with `hydrate-never` will cause it to hydrate)
|
|
143
|
+
::
|
|
144
|
+
|
|
136
145
|
::warning
|
|
137
146
|
Currently Nuxt's built-in lazy hydration only works in single-file components (SFCs), and requires you to define the prop in the template (rather than spreading an object of props via `v-bind`). It also does not work with direct imports from `#components`.
|
|
138
147
|
::
|
|
@@ -231,9 +240,10 @@ Hydrates the component based on a boolean condition.
|
|
|
231
240
|
<LazyMyComponent :hydrate-when="isReady" />
|
|
232
241
|
</div>
|
|
233
242
|
</template>
|
|
243
|
+
|
|
234
244
|
<script setup lang="ts">
|
|
235
245
|
const isReady = ref(false)
|
|
236
|
-
function myFunction() {
|
|
246
|
+
function myFunction () {
|
|
237
247
|
// trigger custom hydration strategy...
|
|
238
248
|
isReady.value = true
|
|
239
249
|
}
|
|
@@ -259,13 +269,16 @@ All delayed hydration components emit a `@hydrated` event when they are hydrated
|
|
|
259
269
|
```vue [app/pages/index.vue]
|
|
260
270
|
<template>
|
|
261
271
|
<div>
|
|
262
|
-
<LazyMyComponent
|
|
272
|
+
<LazyMyComponent
|
|
273
|
+
hydrate-on-visible
|
|
274
|
+
@hydrated="onHydrate"
|
|
275
|
+
/>
|
|
263
276
|
</div>
|
|
264
277
|
</template>
|
|
265
278
|
|
|
266
279
|
<script setup lang="ts">
|
|
267
|
-
function onHydrate() {
|
|
268
|
-
console.log(
|
|
280
|
+
function onHydrate () {
|
|
281
|
+
console.log('Component has been hydrated!')
|
|
269
282
|
}
|
|
270
283
|
</script>
|
|
271
284
|
```
|
|
@@ -293,7 +306,7 @@ You can also explicitly import components from `#components` if you want or need
|
|
|
293
306
|
|
|
294
307
|
```vue [app/pages/index.vue]
|
|
295
308
|
<script setup lang="ts">
|
|
296
|
-
import {
|
|
309
|
+
import { LazyMountainsList, NuxtLink } from '#components'
|
|
297
310
|
|
|
298
311
|
const show = ref(false)
|
|
299
312
|
</script>
|
|
@@ -302,7 +315,12 @@ const show = ref(false)
|
|
|
302
315
|
<div>
|
|
303
316
|
<h1>Mountains</h1>
|
|
304
317
|
<LazyMountainsList v-if="show" />
|
|
305
|
-
<button
|
|
318
|
+
<button
|
|
319
|
+
v-if="!show"
|
|
320
|
+
@click="show = true"
|
|
321
|
+
>
|
|
322
|
+
Show List
|
|
323
|
+
</button>
|
|
306
324
|
<NuxtLink to="/">Home</NuxtLink>
|
|
307
325
|
</div>
|
|
308
326
|
</template>
|
|
@@ -329,8 +347,8 @@ export default defineNuxtConfig({
|
|
|
329
347
|
//
|
|
330
348
|
// ~/components/Btn.vue => <Btn />
|
|
331
349
|
// ~/components/base/Btn.vue => <BaseBtn />
|
|
332
|
-
'~/components'
|
|
333
|
-
]
|
|
350
|
+
'~/components',
|
|
351
|
+
],
|
|
334
352
|
})
|
|
335
353
|
```
|
|
336
354
|
|
|
@@ -340,7 +358,7 @@ Any nested directories need to be added first as they are scanned in order.
|
|
|
340
358
|
|
|
341
359
|
## npm Packages
|
|
342
360
|
|
|
343
|
-
If you want to auto-import components from an npm package, you can use [`addComponent`](/docs/api/kit/components#addcomponent) in a [local module](/docs/guide/directory-structure/modules) to register them.
|
|
361
|
+
If you want to auto-import components from an npm package, you can use [`addComponent`](/docs/4.x/api/kit/components#addcomponent) in a [local module](/docs/4.x/guide/directory-structure/modules) to register them.
|
|
344
362
|
|
|
345
363
|
::code-group
|
|
346
364
|
|
|
@@ -348,7 +366,7 @@ If you want to auto-import components from an npm package, you can use [`addComp
|
|
|
348
366
|
import { addComponent, defineNuxtModule } from '@nuxt/kit'
|
|
349
367
|
|
|
350
368
|
export default defineNuxtModule({
|
|
351
|
-
setup() {
|
|
369
|
+
setup () {
|
|
352
370
|
// import { MyComponent as MyAutoImportedComponent } from 'my-npm-package'
|
|
353
371
|
addComponent({
|
|
354
372
|
name: 'MyAutoImportedComponent',
|
|
@@ -372,7 +390,7 @@ export default defineNuxtModule({
|
|
|
372
390
|
|
|
373
391
|
## Component Extensions
|
|
374
392
|
|
|
375
|
-
By default, any file with an extension specified in the [extensions key of `nuxt.config.ts`](/docs/api/nuxt-config#extensions) is treated as a component.
|
|
393
|
+
By default, any file with an extension specified in the [extensions key of `nuxt.config.ts`](/docs/4.x/api/nuxt-config#extensions) is treated as a component.
|
|
376
394
|
If you need to restrict the file extensions that should be registered as components, you can use the extended form of the components directory declaration and its `extensions` key:
|
|
377
395
|
|
|
378
396
|
```ts twoslash [nuxt.config.ts]
|
|
@@ -381,8 +399,8 @@ export default defineNuxtConfig({
|
|
|
381
399
|
{
|
|
382
400
|
path: '~/components',
|
|
383
401
|
extensions: ['.vue'], // [!code ++]
|
|
384
|
-
}
|
|
385
|
-
]
|
|
402
|
+
},
|
|
403
|
+
],
|
|
386
404
|
})
|
|
387
405
|
```
|
|
388
406
|
|
|
@@ -412,7 +430,7 @@ This feature only works with Nuxt auto-imports and `#components` imports. Explic
|
|
|
412
430
|
`.client` components are rendered only after being mounted. To access the rendered template using `onMounted()`, add `await nextTick()` in the callback of the `onMounted()` hook.
|
|
413
431
|
::
|
|
414
432
|
|
|
415
|
-
::read-more{to="/docs/api/components/client-only"}
|
|
433
|
+
::read-more{to="/docs/4.x/api/components/client-only"}
|
|
416
434
|
You can also achieve a similar result with the `<ClientOnly>` component.
|
|
417
435
|
::
|
|
418
436
|
|
|
@@ -420,7 +438,7 @@ You can also achieve a similar result with the `<ClientOnly>` component.
|
|
|
420
438
|
|
|
421
439
|
Server components allow server-rendering individual components within your client-side apps. It's possible to use server components within Nuxt, even if you are generating a static site. That makes it possible to build complex sites that mix dynamic components, server-rendered HTML and even static chunks of markup.
|
|
422
440
|
|
|
423
|
-
Server components can either be used on their own or paired with a [client component](#paired-with-a-client-component).
|
|
441
|
+
Server components can either be used on their own or paired with a [client component](/docs/4.x/guide/directory-structure/app/components#paired-with-a-client-component).
|
|
424
442
|
|
|
425
443
|
:video-accordion{title="Watch Learn Vue video about Nuxt Server Components" videoId="u1yyXe86xJM"}
|
|
426
444
|
|
|
@@ -439,8 +457,8 @@ Server components are currently experimental and in order to use them, you need
|
|
|
439
457
|
```ts twoslash [nuxt.config.ts]
|
|
440
458
|
export default defineNuxtConfig({
|
|
441
459
|
experimental: {
|
|
442
|
-
componentIslands: true
|
|
443
|
-
}
|
|
460
|
+
componentIslands: true,
|
|
461
|
+
},
|
|
444
462
|
})
|
|
445
463
|
```
|
|
446
464
|
|
|
@@ -463,7 +481,7 @@ Now you can register server-only components with the `.server` suffix and use th
|
|
|
463
481
|
</template>
|
|
464
482
|
```
|
|
465
483
|
|
|
466
|
-
Server-only components use [`<NuxtIsland>`](/docs/api/components/nuxt-island) under the hood, meaning that `lazy` prop and `#fallback` slot are both passed down to it.
|
|
484
|
+
Server-only components use [`<NuxtIsland>`](/docs/4.x/api/components/nuxt-island) under the hood, meaning that `lazy` prop and `#fallback` slot are both passed down to it.
|
|
467
485
|
|
|
468
486
|
::warning
|
|
469
487
|
Server components (and islands) must have a single root element. (HTML comments are considered elements as well.)
|
|
@@ -494,7 +512,10 @@ You can partially hydrate a component by setting a `nuxt-client` attribute on th
|
|
|
494
512
|
<div>
|
|
495
513
|
<HighlightedMarkdown markdown="# Headline" />
|
|
496
514
|
<!-- Counter will be loaded and hydrated client-side -->
|
|
497
|
-
<Counter
|
|
515
|
+
<Counter
|
|
516
|
+
nuxt-client
|
|
517
|
+
:count="5"
|
|
518
|
+
/>
|
|
498
519
|
</div>
|
|
499
520
|
</template>
|
|
500
521
|
```
|
|
@@ -543,14 +564,14 @@ In this case, the `.server` + `.client` components are two 'halves' of a compone
|
|
|
543
564
|
|
|
544
565
|
There are a number of components that Nuxt provides, including `<ClientOnly>` and `<DevOnly>`. You can read more about them in the API documentation.
|
|
545
566
|
|
|
546
|
-
::read-more{to="/docs/api"}
|
|
567
|
+
::read-more{to="/docs/4.x/api"}
|
|
547
568
|
::
|
|
548
569
|
|
|
549
570
|
## Library Authors
|
|
550
571
|
|
|
551
572
|
Making Vue component libraries with automatic tree-shaking and component registration is super easy. ✨
|
|
552
573
|
|
|
553
|
-
You can use the [`addComponentsDir`](/docs/api/kit/components#addcomponentsdir) method provided from the `@nuxt/kit` to register your components directory in your Nuxt module.
|
|
574
|
+
You can use the [`addComponentsDir`](/docs/4.x/api/kit/components#addcomponentsdir) method provided from the `@nuxt/kit` to register your components directory in your Nuxt module.
|
|
554
575
|
|
|
555
576
|
Imagine a directory structure like this:
|
|
556
577
|
|
|
@@ -569,10 +590,10 @@ Imagine a directory structure like this:
|
|
|
569
590
|
Then in `awesome-ui/nuxt.ts` you can use the `addComponentsDir` hook:
|
|
570
591
|
|
|
571
592
|
```ts twoslash
|
|
572
|
-
import { createResolver, defineNuxtModule
|
|
593
|
+
import { addComponentsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
573
594
|
|
|
574
595
|
export default defineNuxtModule({
|
|
575
|
-
setup() {
|
|
596
|
+
setup () {
|
|
576
597
|
const resolver = createResolver(import.meta.url)
|
|
577
598
|
|
|
578
599
|
// Add ./components dir to the list
|
|
@@ -588,7 +609,7 @@ That's it! Now in your project, you can import your UI library as a Nuxt module
|
|
|
588
609
|
|
|
589
610
|
```ts twoslash [nuxt.config.ts]
|
|
590
611
|
export default defineNuxtConfig({
|
|
591
|
-
modules: ['awesome-ui/nuxt']
|
|
612
|
+
modules: ['awesome-ui/nuxt'],
|
|
592
613
|
})
|
|
593
614
|
```
|
|
594
615
|
|
|
@@ -605,4 +626,4 @@ export default defineNuxtConfig({
|
|
|
605
626
|
|
|
606
627
|
It will automatically import the components only if used and also support HMR when updating your components in `node_modules/awesome-ui/components/`.
|
|
607
628
|
|
|
608
|
-
:link-example{to="/docs/examples/features/auto-imports"}
|
|
629
|
+
:link-example{to="/docs/4.x/examples/features/auto-imports"}
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
title: 'composables'
|
|
3
3
|
head.title: 'composables/'
|
|
4
4
|
description: Use the composables/ directory to auto-import your Vue composables into your application.
|
|
5
|
-
navigation.icon: i-
|
|
5
|
+
navigation.icon: i-vscode-icons-folder-type-src
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
## Usage
|
|
9
9
|
|
|
10
10
|
**Method 1:** Using named export
|
|
11
11
|
|
|
12
|
-
```
|
|
12
|
+
```ts [app/composables/useFoo.ts]
|
|
13
13
|
export const useFoo = () => {
|
|
14
14
|
return useState('foo', () => 'bar')
|
|
15
15
|
}
|
|
@@ -17,7 +17,7 @@ export const useFoo = () => {
|
|
|
17
17
|
|
|
18
18
|
**Method 2:** Using default export
|
|
19
19
|
|
|
20
|
-
```
|
|
20
|
+
```ts [app/composables/use-foo.ts or composables/useFoo.ts]
|
|
21
21
|
// It will be available as useFoo() (camelCase of file name without extension)
|
|
22
22
|
export default function () {
|
|
23
23
|
return useState('foo', () => 'bar')
|
|
@@ -42,15 +42,15 @@ const foo = useFoo()
|
|
|
42
42
|
The `app/composables/` directory in Nuxt does not provide any additional reactivity capabilities to your code. Instead, any reactivity within composables is achieved using Vue's Composition API mechanisms, such as ref and reactive. Note that reactive code is also not limited to the boundaries of the `app/composables/` directory. You are free to employ reactivity features wherever they're needed in your application.
|
|
43
43
|
::
|
|
44
44
|
|
|
45
|
-
:read-more{to="/docs/guide/concepts/auto-imports"}
|
|
45
|
+
:read-more{to="/docs/4.x/guide/concepts/auto-imports"}
|
|
46
46
|
|
|
47
|
-
:link-example{to="/docs/examples/features/auto-imports"}
|
|
47
|
+
:link-example{to="/docs/4.x/examples/features/auto-imports"}
|
|
48
48
|
|
|
49
49
|
## Types
|
|
50
50
|
|
|
51
51
|
Under the hood, Nuxt auto generates the file `.nuxt/imports.d.ts` to declare the types.
|
|
52
52
|
|
|
53
|
-
Be aware that you have to run [`nuxt prepare`](/docs/api/commands/prepare), [`nuxt dev`](/docs/api/commands/dev) or [`nuxt build`](/docs/api/commands/build) in order to let Nuxt generate the types.
|
|
53
|
+
Be aware that you have to run [`nuxt prepare`](/docs/4.x/api/commands/prepare), [`nuxt dev`](/docs/4.x/api/commands/dev) or [`nuxt build`](/docs/4.x/api/commands/build) in order to let Nuxt generate the types.
|
|
54
54
|
|
|
55
55
|
::note
|
|
56
56
|
If you create a composable without having the dev server running, TypeScript will throw an error, such as `Cannot find name 'useBar'.`
|
|
@@ -62,7 +62,7 @@ If you create a composable without having the dev server running, TypeScript wil
|
|
|
62
62
|
|
|
63
63
|
You can use a composable within another composable using auto imports:
|
|
64
64
|
|
|
65
|
-
```
|
|
65
|
+
```ts [app/composables/test.ts]
|
|
66
66
|
export const useFoo = () => {
|
|
67
67
|
const nuxtApp = useNuxtApp()
|
|
68
68
|
const bar = useBar()
|
|
@@ -71,9 +71,9 @@ export const useFoo = () => {
|
|
|
71
71
|
|
|
72
72
|
### Access plugin injections
|
|
73
73
|
|
|
74
|
-
You can access [plugin injections](/docs/guide/directory-structure/plugins#providing-helpers) from composables:
|
|
74
|
+
You can access [plugin injections](/docs/4.x/guide/directory-structure/plugins#providing-helpers) from composables:
|
|
75
75
|
|
|
76
|
-
```
|
|
76
|
+
```ts [app/composables/test.ts]
|
|
77
77
|
export const useHello = () => {
|
|
78
78
|
const nuxtApp = useNuxtApp()
|
|
79
79
|
return nuxtApp.$hello
|
|
@@ -82,7 +82,7 @@ export const useHello = () => {
|
|
|
82
82
|
|
|
83
83
|
## How Files Are Scanned
|
|
84
84
|
|
|
85
|
-
Nuxt only scans files at the top level of the [`app/composables/` directory](/docs/guide/directory-structure/app/composables), e.g.:
|
|
85
|
+
Nuxt only scans files at the top level of the [`app/composables/` directory](/docs/4.x/guide/directory-structure/app/composables), e.g.:
|
|
86
86
|
|
|
87
87
|
```bash [Directory Structure]
|
|
88
88
|
-| composables/
|
|
@@ -114,8 +114,8 @@ export default defineNuxtConfig({
|
|
|
114
114
|
// ... or scan composables nested one level deep with a specific name and file extension
|
|
115
115
|
'~/composables/*/index.{ts,js,mjs,mts}',
|
|
116
116
|
// ... or scan all composables within given directory
|
|
117
|
-
'~/composables/**'
|
|
118
|
-
]
|
|
119
|
-
}
|
|
117
|
+
'~/composables/**',
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
120
|
})
|
|
121
121
|
```
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
title: "layouts"
|
|
3
3
|
head.title: "layouts/"
|
|
4
4
|
description: "Nuxt provides a layouts framework to extract common UI patterns into reusable layouts."
|
|
5
|
-
navigation.icon: i-
|
|
5
|
+
navigation.icon: i-vscode-icons-folder-type-view
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
::tip{icon="i-lucide-rocket" }
|
|
@@ -11,7 +11,7 @@ For best performance, components placed in this directory will be automatically
|
|
|
11
11
|
|
|
12
12
|
## Enable Layouts
|
|
13
13
|
|
|
14
|
-
Layouts are enabled by adding [`<NuxtLayout>`](/docs/api/components/nuxt-layout) to your [`app.vue`](/docs/guide/directory-structure/app):
|
|
14
|
+
Layouts are enabled by adding [`<NuxtLayout>`](/docs/4.x/api/components/nuxt-layout) to your [`app.vue`](/docs/4.x/guide/directory-structure/app):
|
|
15
15
|
|
|
16
16
|
```vue [app/app.vue]
|
|
17
17
|
<template>
|
|
@@ -22,7 +22,7 @@ Layouts are enabled by adding [`<NuxtLayout>`](/docs/api/components/nuxt-layout)
|
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
To use a layout:
|
|
25
|
-
- Set a `layout` property in your page with [definePageMeta](/docs/api/utils/define-page-meta).
|
|
25
|
+
- Set a `layout` property in your page with [definePageMeta](/docs/4.x/api/utils/define-page-meta).
|
|
26
26
|
- Set the `name` prop of `<NuxtLayout>`.
|
|
27
27
|
|
|
28
28
|
::note
|
|
@@ -34,7 +34,7 @@ If no layout is specified, `app/layouts/default.vue` will be used.
|
|
|
34
34
|
::
|
|
35
35
|
|
|
36
36
|
::important
|
|
37
|
-
If you only have a single layout in your application, we recommend using [`app.vue`](/docs/guide/directory-structure/app) instead.
|
|
37
|
+
If you only have a single layout in your application, we recommend using [`app.vue`](/docs/4.x/guide/directory-structure/app) instead.
|
|
38
38
|
::
|
|
39
39
|
|
|
40
40
|
::important
|
|
@@ -69,21 +69,21 @@ Then you can use the `custom` layout in your page:
|
|
|
69
69
|
```vue twoslash [pages/about.vue]
|
|
70
70
|
<script setup lang="ts">
|
|
71
71
|
definePageMeta({
|
|
72
|
-
layout: 'custom'
|
|
72
|
+
layout: 'custom',
|
|
73
73
|
})
|
|
74
74
|
</script>
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
::read-more{to="/docs/guide/directory-structure/app/pages#page-metadata"}
|
|
77
|
+
::read-more{to="/docs/4.x/guide/directory-structure/app/pages#page-metadata"}
|
|
78
78
|
Learn more about `definePageMeta`.
|
|
79
79
|
::
|
|
80
80
|
|
|
81
|
-
You can directly override the default layout for all pages using the `name` property of [`<NuxtLayout>`](/docs/api/components/nuxt-layout):
|
|
81
|
+
You can directly override the default layout for all pages using the `name` property of [`<NuxtLayout>`](/docs/4.x/api/components/nuxt-layout):
|
|
82
82
|
|
|
83
83
|
```vue [app/app.vue]
|
|
84
84
|
<script setup lang="ts">
|
|
85
85
|
// You might choose this based on an API call or logged-in status
|
|
86
|
-
const layout =
|
|
86
|
+
const layout = 'custom'
|
|
87
87
|
</script>
|
|
88
88
|
|
|
89
89
|
<template>
|
|
@@ -109,11 +109,11 @@ File | Layout Name
|
|
|
109
109
|
`~/layouts/desktop-base/DesktopBase.vue` | `desktop-base`
|
|
110
110
|
`~/layouts/desktop/Desktop.vue` | `desktop`
|
|
111
111
|
|
|
112
|
-
:link-example{to="/docs/examples/features/layouts"}
|
|
112
|
+
:link-example{to="/docs/4.x/examples/features/layouts"}
|
|
113
113
|
|
|
114
114
|
## Changing the Layout Dynamically
|
|
115
115
|
|
|
116
|
-
You can also use the [`setPageLayout`](/docs/api/utils/set-page-layout) helper to change the layout dynamically:
|
|
116
|
+
You can also use the [`setPageLayout`](/docs/4.x/api/utils/set-page-layout) helper to change the layout dynamically:
|
|
117
117
|
|
|
118
118
|
```vue twoslash
|
|
119
119
|
<script setup lang="ts">
|
|
@@ -122,17 +122,19 @@ function enableCustomLayout () {
|
|
|
122
122
|
}
|
|
123
123
|
definePageMeta({
|
|
124
124
|
layout: false,
|
|
125
|
-
})
|
|
125
|
+
})
|
|
126
126
|
</script>
|
|
127
127
|
|
|
128
128
|
<template>
|
|
129
129
|
<div>
|
|
130
|
-
<button @click="enableCustomLayout">
|
|
130
|
+
<button @click="enableCustomLayout">
|
|
131
|
+
Update layout
|
|
132
|
+
</button>
|
|
131
133
|
</div>
|
|
132
134
|
</template>
|
|
133
135
|
```
|
|
134
136
|
|
|
135
|
-
:link-example{to="/docs/examples/features/layouts"}
|
|
137
|
+
:link-example{to="/docs/4.x/examples/features/layouts"}
|
|
136
138
|
|
|
137
139
|
## Overriding a Layout on a Per-page Basis
|
|
138
140
|
|
|
@@ -150,7 +152,9 @@ definePageMeta({
|
|
|
150
152
|
<template>
|
|
151
153
|
<div>
|
|
152
154
|
<NuxtLayout name="custom">
|
|
153
|
-
<template #header>
|
|
155
|
+
<template #header>
|
|
156
|
+
Some header template content.
|
|
157
|
+
</template>
|
|
154
158
|
|
|
155
159
|
The rest of the page
|
|
156
160
|
</NuxtLayout>
|
|
@@ -176,5 +180,5 @@ definePageMeta({
|
|
|
176
180
|
::
|
|
177
181
|
|
|
178
182
|
::important
|
|
179
|
-
If you use `<NuxtLayout>` within your pages, make sure it is not the root element (or [disable layout/page transitions](/docs/getting-started/transitions#disable-transitions)).
|
|
183
|
+
If you use `<NuxtLayout>` within your pages, make sure it is not the root element (or [disable layout/page transitions](/docs/4.x/getting-started/transitions#disable-transitions)).
|
|
180
184
|
::
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
title: "middleware"
|
|
3
3
|
description: "Nuxt provides middleware to run code before navigating to a particular route."
|
|
4
4
|
head.title: "middleware/"
|
|
5
|
-
navigation.icon: i-
|
|
5
|
+
navigation.icon: i-vscode-icons-folder-type-middleware
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
Nuxt provides a customizable **route middleware** framework you can use throughout your application, ideal for extracting code that you want to run before navigating to a particular route.
|
|
@@ -13,14 +13,14 @@ There are three kinds of route middleware:
|
|
|
13
13
|
2. Named route middleware, placed in the `app/middleware/` and automatically loaded via asynchronous import when used on a page.
|
|
14
14
|
3. Global route middleware, placed in the `app/middleware/` with a `.global` suffix and is run on every route change.
|
|
15
15
|
|
|
16
|
-
The first two kinds of route middleware can be defined in [`definePageMeta`](/docs/api/utils/define-page-meta).
|
|
16
|
+
The first two kinds of route middleware can be defined in [`definePageMeta`](/docs/4.x/api/utils/define-page-meta).
|
|
17
17
|
|
|
18
18
|
::note
|
|
19
19
|
Name of middleware are normalized to kebab-case: `myMiddleware` becomes `my-middleware`.
|
|
20
20
|
::
|
|
21
21
|
|
|
22
22
|
::note
|
|
23
|
-
Route middleware run within the Vue part of your Nuxt app. Despite the similar name, they are completely different from [server middleware](/docs/guide/directory-structure/server#server-middleware), which are run in the Nitro server part of your app.
|
|
23
|
+
Route middleware run within the Vue part of your Nuxt app. Despite the similar name, they are completely different from [server middleware](/docs/4.x/guide/directory-structure/server#server-middleware), which are run in the Nitro server part of your app.
|
|
24
24
|
::
|
|
25
25
|
|
|
26
26
|
:video-accordion{title="Watch a video from Vue School on all 3 kinds of middleware" videoId="761471577" platform="vimeo"}
|
|
@@ -45,8 +45,8 @@ export default defineNuxtRouteMiddleware((to, from) => {
|
|
|
45
45
|
|
|
46
46
|
Nuxt provides two globally available helpers that can be returned directly from the middleware.
|
|
47
47
|
|
|
48
|
-
1. [`navigateTo`](/docs/api/utils/navigate-to) - Redirects to the given route
|
|
49
|
-
2. [`abortNavigation`](/docs/api/utils/abort-navigation) - Aborts the navigation, with an optional error message.
|
|
48
|
+
1. [`navigateTo`](/docs/4.x/api/utils/navigate-to) - Redirects to the given route
|
|
49
|
+
2. [`abortNavigation`](/docs/4.x/api/utils/abort-navigation) - Aborts the navigation, with an optional error message.
|
|
50
50
|
|
|
51
51
|
Unlike [navigation guards](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) from `vue-router`, a third `next()` argument is not passed, and **redirect or route cancellation is handled by returning a value from the middleware**.
|
|
52
52
|
|
|
@@ -58,8 +58,8 @@ Possible return values are:
|
|
|
58
58
|
* `return abortNavigation()` - stops the current navigation
|
|
59
59
|
* `return abortNavigation(error)` - rejects the current navigation with an error
|
|
60
60
|
|
|
61
|
-
:read-more{to="/docs/api/utils/navigate-to"}
|
|
62
|
-
:read-more{to="/docs/api/utils/abort-navigation"}
|
|
61
|
+
:read-more{to="/docs/4.x/api/utils/navigate-to"}
|
|
62
|
+
:read-more{to="/docs/4.x/api/utils/abort-navigation"}
|
|
63
63
|
|
|
64
64
|
::important
|
|
65
65
|
We recommend using the helper functions above for performing redirects or stopping navigation. Other possible return values described in [the vue-router docs](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) may work but there may be breaking changes in future.
|
|
@@ -90,7 +90,7 @@ definePageMeta({
|
|
|
90
90
|
},
|
|
91
91
|
'auth',
|
|
92
92
|
],
|
|
93
|
-
})
|
|
93
|
+
})
|
|
94
94
|
</script>
|
|
95
95
|
```
|
|
96
96
|
|
|
@@ -125,26 +125,32 @@ If your site is server-rendered or generated, middleware for the initial page wi
|
|
|
125
125
|
However, if you want to avoid this behaviour you can do so:
|
|
126
126
|
|
|
127
127
|
```ts twoslash [middleware/example.ts]
|
|
128
|
-
export default defineNuxtRouteMiddleware(to => {
|
|
128
|
+
export default defineNuxtRouteMiddleware((to) => {
|
|
129
129
|
// skip middleware on server
|
|
130
|
-
if (import.meta.server)
|
|
130
|
+
if (import.meta.server) {
|
|
131
|
+
return
|
|
132
|
+
}
|
|
131
133
|
// skip middleware on client side entirely
|
|
132
|
-
if (import.meta.client)
|
|
134
|
+
if (import.meta.client) {
|
|
135
|
+
return
|
|
136
|
+
}
|
|
133
137
|
// or only skip middleware on initial client load
|
|
134
138
|
const nuxtApp = useNuxtApp()
|
|
135
|
-
if (import.meta.client && nuxtApp.isHydrating && nuxtApp.payload.serverRendered)
|
|
139
|
+
if (import.meta.client && nuxtApp.isHydrating && nuxtApp.payload.serverRendered) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
136
142
|
})
|
|
137
143
|
```
|
|
138
144
|
|
|
139
145
|
This is true even if you throw an error in your middleware on the server, and an error page is rendered. The middleware will still run again in the browser.
|
|
140
146
|
|
|
141
147
|
::note
|
|
142
|
-
Rendering an error page is an entirely separate page load, meaning any registered middleware will run again. You can use [`useError`](/docs/getting-started/error-handling#useerror) in middleware to check if an error is being handled.
|
|
148
|
+
Rendering an error page is an entirely separate page load, meaning any registered middleware will run again. You can use [`useError`](/docs/4.x/getting-started/error-handling#useerror) in middleware to check if an error is being handled.
|
|
143
149
|
::
|
|
144
150
|
|
|
145
151
|
## Accessing Route in Middleware
|
|
146
152
|
|
|
147
|
-
Always use the `to` and `from` parameters in your middleware to access the next and previous routes. Avoid using the [`useRoute()`](/docs/api/composables/use-route) composable in this context altogether.
|
|
153
|
+
Always use the `to` and `from` parameters in your middleware to access the next and previous routes. Avoid using the [`useRoute()`](/docs/4.x/api/composables/use-route) composable in this context altogether.
|
|
148
154
|
There is **no concept of a "current route" in middleware**, as middleware can abort a navigation or redirect to a different route. The `useRoute()` composable will always be inaccurate in this context.
|
|
149
155
|
|
|
150
156
|
::warning
|
|
@@ -155,10 +161,10 @@ This leads to the **same issue as above**, so you should structure your function
|
|
|
155
161
|
::code-group
|
|
156
162
|
```ts twoslash [middleware/access-route.ts]
|
|
157
163
|
// @errors: 2304
|
|
158
|
-
export default defineNuxtRouteMiddleware(to => {
|
|
164
|
+
export default defineNuxtRouteMiddleware((to) => {
|
|
159
165
|
// passing the route to the function to avoid calling `useRoute()` in middleware
|
|
160
166
|
doSomethingWithRoute(to)
|
|
161
|
-
|
|
167
|
+
|
|
162
168
|
// ❌ this will output a warning and is NOT recommended
|
|
163
169
|
callsRouteInternally()
|
|
164
170
|
})
|
|
@@ -166,14 +172,14 @@ export default defineNuxtRouteMiddleware(to => {
|
|
|
166
172
|
|
|
167
173
|
```ts twoslash [utils/handle-route.ts]
|
|
168
174
|
// providing the route as an argument so that it can be used in middleware correctly
|
|
169
|
-
export function doSomethingWithRoute(route = useRoute()) {
|
|
175
|
+
export function doSomethingWithRoute (route = useRoute()) {
|
|
170
176
|
// ...
|
|
171
177
|
}
|
|
172
178
|
```
|
|
173
179
|
```ts twoslash [utils/dont-do-this.ts]
|
|
174
180
|
// ❌ this function is not suitable for use in middleware
|
|
175
|
-
export function callsRouteInternally() {
|
|
176
|
-
|
|
181
|
+
export function callsRouteInternally () {
|
|
182
|
+
const route = useRoute()
|
|
177
183
|
// ...
|
|
178
184
|
}
|
|
179
185
|
```
|
|
@@ -182,7 +188,7 @@ export function callsRouteInternally() {
|
|
|
182
188
|
|
|
183
189
|
## Adding Middleware Dynamically
|
|
184
190
|
|
|
185
|
-
It is possible to add global or named route middleware manually using the [`addRouteMiddleware()`](/docs/api/utils/add-route-middleware) helper function, such as from within a plugin.
|
|
191
|
+
It is possible to add global or named route middleware manually using the [`addRouteMiddleware()`](/docs/4.x/api/utils/add-route-middleware) helper function, such as from within a plugin.
|
|
186
192
|
|
|
187
193
|
```ts twoslash
|
|
188
194
|
export default defineNuxtPlugin(() => {
|
|
@@ -208,7 +214,7 @@ In your page file, you can reference this route middleware:
|
|
|
208
214
|
```vue twoslash
|
|
209
215
|
<script setup lang="ts">
|
|
210
216
|
definePageMeta({
|
|
211
|
-
middleware: [
|
|
217
|
+
middleware: ['auth'],
|
|
212
218
|
// or middleware: 'auth'
|
|
213
219
|
})
|
|
214
220
|
</script>
|
|
@@ -216,7 +222,7 @@ definePageMeta({
|
|
|
216
222
|
|
|
217
223
|
Now, before navigation to that page can complete, the `auth` route middleware will be run.
|
|
218
224
|
|
|
219
|
-
:link-example{to="/docs/examples/routing/middleware"}
|
|
225
|
+
:link-example{to="/docs/4.x/examples/routing/middleware"}
|
|
220
226
|
|
|
221
227
|
## Setting Middleware at Build Time
|
|
222
228
|
|
|
@@ -230,7 +236,7 @@ export default defineNuxtConfig({
|
|
|
230
236
|
'pages:extend' (pages) {
|
|
231
237
|
function setMiddleware (pages: NuxtPage[]) {
|
|
232
238
|
for (const page of pages) {
|
|
233
|
-
if (/* some condition */
|
|
239
|
+
if (/* some condition */ Math.random() > 0.5) {
|
|
234
240
|
page.meta ||= {}
|
|
235
241
|
// Note that this will override any middleware set in `definePageMeta` in the page
|
|
236
242
|
page.meta.middleware = ['named']
|
|
@@ -241,7 +247,7 @@ export default defineNuxtConfig({
|
|
|
241
247
|
}
|
|
242
248
|
}
|
|
243
249
|
setMiddleware(pages)
|
|
244
|
-
}
|
|
245
|
-
}
|
|
250
|
+
},
|
|
251
|
+
},
|
|
246
252
|
})
|
|
247
253
|
```
|