<NuxtPage> is a built-in component that comes with Nuxt. It lets you display top-level or nested pages located in the app/pages/ directory.
<NuxtPage> is a wrapper around <RouterView> from Vue Router. It should be used instead of <RouterView> because the former takes additional care of internal states. Otherwise, useRoute() may return incorrect paths.Usage
<NuxtPage> includes the following components:
<Transition> and <KeepAlive>. You can enable them in the nuxt.config file or by setting the transition and keepalive properties on <NuxtPage>. If you want to define a specific page, you can set it in definePageMeta in the page component.
Since <NuxtPage> uses <Suspense> under the hood, the component lifecycle behavior during page changes differs from that of a typical Vue application.
In a typical Vue application, a new page component is mounted only after the previous one has been fully unmounted. However, in Nuxt, due to how Vue <Suspense> is implemented, the new page component is mounted before the previous one is unmounted.
Props
Tells
<RouterView> to render the component with the corresponding name in the matched route record’s components option.Route location that has all of its components resolved.
Control when the
NuxtPage component is re-rendered.Define global transitions for all pages rendered with the
NuxtPage component. See Vue’s TransitionProps.Control state preservation of pages rendered with the
NuxtPage component. See Vue’s KeepAliveProps.Example
For example, if you pass a key that never changes, the<NuxtPage> component will be rendered only once - when it is first mounted.
[app/app.vue]
pageKey can be passed as a key value via definePageMeta from the <script> section of your Vue component in the /pages directory.
[app/pages/my-page.vue]
Page’s Ref
To get theref of a page component, access it through ref.value.pageRef
[app/app.vue]
[my-page.vue]
Custom Props
<NuxtPage> also accepts custom props that you may need to pass further down the hierarchy.
For example, in the below example, the value of foobar will be passed to the NuxtPage component and then to the page components.
[app/app.vue]
foobar prop in the page component:
[app/pages/page.vue]
defineProps, any props passed down to NuxtPage can still be accessed directly from the page attrs:
[app/pages/page.vue]