Pages directory

Nuxt provides a file-based routing to create routes within your web application using Vue Router under the hood.

This directory is optional, meaning that vue-router won’t be included if you only use app.vue, reducing your application’s bundle size.

Usage

Pages are Vue components and can have the .vue, .js, .jsx, .ts or .tsx extension.

pages/index.vue

  1. <template>
  2. <h1>Index page</h1>
  3. </template>

pages/index.ts

  1. // https://vuejs.org/guide/extras/render-function.html
  2. export default defineComponent({
  3. render () {
  4. return h('h1', 'Index page')
  5. }
  6. })

pages/index.tsx

  1. // https://vuejs.org/guide/extras/render-function.html#jsx-tsx
  2. export default defineComponent({
  3. render () {
  4. return <h1>Index page</h1>
  5. }
  6. })

The pages/index.vue file will be mapped to the / route of your application.

If you are using app.vue, make sure to use the <NuxtPage/> component to display the current page:

app.vue

  1. <template>
  2. <div>
  3. <!-- Markup shared across all pages, ex: NavBar -->
  4. <NuxtPage />
  5. </div>
  6. </template>

Pages must have a single root element to allow route transitions between pages. (HTML comments are considered elements as well.)

This means that when the route is server-rendered, or statically generated, you will be able to see its contents correctly, but when you navigate towards that route during client-side navigation the transition between routes will fail and you’ll see that the route will not be rendered.

Here are some examples to illustrate what a page with a single root element looks like:

pages/working.vue

  1. <template>
  2. <div>
  3. <!-- This page correctly has only one single root element -->
  4. Page content
  5. </div>
  6. </template>

pages/bad-1.vue

  1. <template>
  2. <!-- This page will not render when route changes during client side navigation, because of this comment -->
  3. <div>Page content</div>
  4. </template>

pages/bad-2.vue

  1. <template>
  2. <div>This page</div>
  3. <div>Has more than one root element</div>
  4. <div>And will not render when route changes during client side navigation</div>
  5. </template>

Dynamic Routes

If you place anything within square brackets, it will be turned into a dynamic route parameter. You can mix and match multiple parameters and even non-dynamic text within a file name or directory.

Example

  1. -| pages/
  2. ---| index.vue
  3. ---| users-[group]/
  4. -----| [id].vue

Given the example above, you can access group/id within your component via the $route object:

pages/users-[group]/[id].vue

  1. <template>
  2. <p>{{ $route.params.group }} - {{ $route.params.id }}</p>
  3. </template>

Navigating to /users-admins/123 would render:

  1. <p>admins - 123</p>

If you want to access the route using Composition API, there is a global useRoute function that will allow you to access the route just like this.$route in the Options API.

  1. <script setup>
  2. const route = useRoute()
  3. if (route.params.group === 'admins' && !route.params.id) {
  4. console.log('Warning! Make sure user is authenticated!')
  5. }
  6. </script>

Catch all route

If you need a catch-all route, you create it by using a file named like [...slug].vue. This will match all routes under that path.

pages/[…slug].vue

  1. <template>
  2. <p>{{ $route.params.slug }}</p>
  3. </template>

Navigating to /hello/world would render:

  1. <p>["hello", "world"]</p>

Nested Routes

It is possible to display nested routes with <NuxtPage>.

Example:

  1. -| pages/
  2. ---| parent/
  3. ------| child.vue
  4. ---| parent.vue

This file tree will generate these routes:

  1. [
  2. {
  3. path: '/parent',
  4. component: '~/pages/parent.vue',
  5. name: 'parent',
  6. children: [
  7. {
  8. path: 'child',
  9. component: '~/pages/parent/child.vue',
  10. name: 'parent-child'
  11. }
  12. ]
  13. }
  14. ]

To display the child.vue component, you have to insert the <NuxtPage> component inside pages/parent.vue:

pages/parent.vue

  1. <template>
  2. <div>
  3. <h1>I am the parent view</h1>
  4. <NuxtPage :foobar="123" />
  5. </div>
  6. </template>

Child route keys

If you want more control over when the <NuxtPage> component is re-rendered (for example, for transitions), you can either pass a string or function via the pageKey prop, or you can define a key value via definePageMeta:

pages/parent.vue

  1. <template>
  2. <div>
  3. <h1>I am the parent view</h1>
  4. <NuxtPage :page-key="someKey" />
  5. </div>
  6. </template>

Or alternatively:

pages/child.vue

  1. <script setup>
  2. definePageMeta({
  3. key: route => route.fullPath
  4. })
  5. </script>

🔎

Read and edit a live example in Examples > Routing > Pages

Page Metadata

You might want to define metadata for each route in your app. You can do this using the definePageMeta macro, which will work both in <script> and in <script setup>:

  1. <script setup>
  2. definePageMeta({
  3. title: 'My home page'
  4. })
  5. </script>

This data can then be accessed throughout the rest of your app from the route.meta object.

  1. <script setup>
  2. const route = useRoute()
  3. console.log(route.meta.title) // My home page
  4. </script>

If you are using nested routes, the page metadata from all these routes will be merged into a single object. For more on route meta, see the vue-router docs.

Much like defineEmits or defineProps (see Vue docs), definePageMeta is a compiler macro. It will be compiled away so you cannot reference it within your component. Instead, the metadata passed to it will be hoisted out of the component. Therefore, the page meta object cannot reference the component (or values defined on the component). However, it can reference imported bindings.

  1. <script setup>
  2. import { someData } from '~/utils/example'
  3. const title = ref('')
  4. definePageMeta({
  5. title, // This will create an error
  6. someData
  7. })
  8. </script>

Special Metadata

Of course, you are welcome to define metadata for your own use throughout your app. But some metadata defined with definePageMeta has a particular purpose:

keepalive

Nuxt will automatically wrap your page in the Vue component if you set keepalive: true in your definePageMeta. This might be useful to do, for example, in a parent route that has dynamic child routes, if you want to preserve page state across route changes. You can also set props to be passed to <KeepAlive> (see a full list here).

key

See above.

layout

You can define the layout used to render the route. This can be either false (to disable any layout), a string or a ref/computed, if you want to make it reactive in some way. More about layouts.

middleware

You can define middleware to apply before loading this page. It will be merged with all the other middleware used in any matching parent/child routes. It can be a string, a function (an anonymous/inlined middleware function following the global before guard pattern), or an array of strings/functions. More about named middleware.

layoutTransition and pageTransition

You can define transition properties for the <transition> component that wraps your pages and layouts, or pass false to disable the <transition> wrapper for that route. You can see a list of options that can be passed here or read more about how transitions work.

alias

You can define page aliases. They allow you to access the same page from different paths. It can be either a string or an array of strings as defined here on vue-router documentation.

Navigation

To navigate between pages of your app, you should use the component.

This component is included with Nuxt and therefore you don’t have to import it as you do with other components.

A simple link to the index.vue page in your pages folder:

  1. <template>
  2. <NuxtLink to="/">Home page</NuxtLink>
  3. </template>

Learn more about usage.

Router options

It is possible to set default vue-router options.

Note: history and routes options will be always overridden by Nuxt.

Using app/router.options

This is the recommended way to specify router options.

app/router.options.ts

  1. import type { RouterConfig } from '@nuxt/schema'
  2. // https://router.vuejs.org/api/#routeroptions
  3. export default <RouterConfig>{
  4. }

Using nuxt.config

Note: Only JSON serializable options are configurable:

  • linkActiveClass
  • linkExactActiveClass
  • end
  • sensitive
  • strict

nuxt.config

  1. export default defineNuxtConfig({
  2. router: {
  3. // https://router.vuejs.org/api/#routeroptions
  4. options: {}
  5. }
  6. })

Programmatic Navigation

Nuxt 3 allows programmatic navigation through the navigateTo() utility method. Using this utility method, you will be able to programmatically navigate the user in your app. This is great for taking input from the user and navigating them dynamically throughout your application. In this example, we have a simple method called navigation() that gets called when the user submits a search form.

Note: Ensure to always await on navigateTo or chain it’s result by returning from functions.

  1. <script setup>
  2. const router = useRouter();
  3. const name = ref('');
  4. const type = ref(1);
  5. function navigate(){
  6. return navigateTo({
  7. path: '/search',
  8. query: {
  9. name: name.value,
  10. type: type.value
  11. }
  12. })
  13. }
  14. </script>