File-based Routing and Navigation

Learn Nuxt's file-based routing, create pages and dynamic and nested routes, and navigate with NuxtLink.

🎯 Introduction

By the end of this article, you will be able to build a multipage Nuxt application with file-based routing. You will create basic pages, dynamic routes, and nested routes, navigate with <NuxtLink>, and read current route information.


🧱 File-based Routing

Nuxt generates routes from the file structure in app/pages/, rather than requiring you to define them in router/index.js.

Consider the following directory structure:

pages/
├─ index.vue      // Home /
├─ about.vue      // /about
├─ user/
│  ├─ index.vue   // /user
│  ├─ profile.vue // /user/profile
│  ├─ [id].vue    // /user/:id

Nuxt generates the corresponding routes automatically, so you do not need to configure vue-router manually.

Think of pages/ as your site's directory tree: folders define path segments, and .vue files define pages.

📄 Basic Pages

To create a basic page, add a .vue file directly in pages/.

app/pages/about.vue
<template>
    <div>
        <h1>About Us</h1>
        <p>This is the about page</p>
    </div>
</template>

Visit /about to see this content.

pages/index.vue is the default index page for this directory and maps to /.

🔢 Dynamic Routes

Use a dynamic route when a URL segment varies, as in /user/123 and /user/456. Nuxt uses square brackets [] to mark dynamic segments.

<!-- app/pages/user/[id].vue -->
<template>
    <div>
        <h1>User Details</h1>
        <p>User ID:{{ route.params.id }}</p>
    </div>
</template>

<script setup>
const route = useRoute();
</script>

At /user/123, route.params.id is '123'.

useRoute() returns the current route object. The id parameter here is a string; convert it with Number(route.params.id) before using it in arithmetic.

🪺 Nested Routes

Use nested routes when pages share a layout whose inner content changes with the child route.

Directory structure:

pages/
├─ user.vue          // Parent route layout /user
├─ user/
│  ├─ index.vue      // /user
│  ├─ settings.vue   // /user/settings
│  ├─ [id].vue       // /user/:id

When pages/user.vue exists, it acts as the parent route's layout:

app/pages/user.vue
<template>
    <div>
        <h2>User Dashboard</h2>
        <NuxtPage />
    </div>
</template>

At /user/settings, Nuxt renders the shared structure from user.vue and inserts the content of user/settings.vue inside it.

Nuxt recognizes the nested relationship because the parent route file and child route directory share a name: user.vue and user/.

Nuxt provides <NuxtLink> for navigation within your application. It uses client-side routing to navigate without reloading the entire page.

app/pages/index.vue
<template>
    <div>
        <h1>Home</h1>
        <NuxtLink to="/about">About</NuxtLink>
        <NuxtLink :to="`/user/${userId}`">View user details</NuxtLink>
    </div>
</template>

<script setup>
const userId = 123;
</script>
For external links, you can use a regular <a> element, such as <a href="https://nuxt.com">Official website</a>. Use <NuxtLink> for internal routes.

🧭 Accessing the Current Route

Besides route parameters, useRoute() provides the current path, query parameters, and other route information.

app/pages/search.vue
<template>
    <div>
        <p>Search keyword:{{ route.query.keyword }}</p>
    </div>
</template>

<script setup>
const route = useRoute();
</script>

At /search?keyword=nuxt, the page displays “Search keyword: nuxt”.


🚫 404 Pages

You can create a custom 404 page in Nuxt. A simple approach is to add a catch-all route in pages/.

A catch-all route uses the filename [...paramName].vue. The three dots capture all remaining path segments. Choose your own parameter name; common examples include [...slug].vue, [...path].vue, and [...catchAll].vue.

<!-- app/pages/[...slug].vue -->
<template>
    <div>
        <h1>404 - Page Not Found</h1>
        <NuxtLink to="/">Back to home</NuxtLink>
    </div>
</template>
slug is simply a parameter name. You can replace it with any valid identifier, such as path or catchAll. At /aaa/bbb, route.params.slug is the array ['aaa', 'bbb'].

❓ Questions and Answers

Q1: How do pages/user/[id].vue and pages/user-[id].vue differ?

pages/user/[id].vue generates /user/:id. pages/user-[id].vue generates /user-:id, matching URLs such as /user-123.

Q2: Are dynamic route parameters always strings?

For the single-value examples here, route.params.xxx and route.query.xxx are strings. Convert them explicitly when you need numbers.

Q3: When should I use <NuxtLink> or <a>?

Use <NuxtLink> for internal navigation without full-page reloads. Use <a> for external destinations or downloads.

Q4: Does a nested route's parent component need <NuxtPage />?

Yes. Without it, the child route's content will not render.

Q5: Is the name slug required in [...slug].vue?

No. It is a parameter name that you can replace with path, catchAll, or another valid name. At /a/b, route.params.yourParameterName is ['a', 'b'].


🧪 Practice

Create the following route structure in your Nuxt project:

pages/
├─ index.vue
├─ product/
│  ├─ index.vue      // /product
│  ├─ [id].vue       // /product/:id
  1. In product/index.vue, list two product links pointing to /product/1 and /product/2.
  2. In product/[id].vue, read route.params.id and display “Product ID: x”.
  3. Add a <NuxtLink> back to the list at /product.
<!-- app/pages/product/[id].vue -->
<template>
    <div>
        <h1>Product Details</h1>
        <!-- Display the product ID here -->
        <!-- Add a link back to the list here -->
    </div>
</template>

<script setup>
// Get the current route here
</script>

🎉 You now understand Nuxt's file-based routing! Next, we will explore its most commonly used data-fetching composables: useFetch and useAsyncData.