Skip to main content
InterviewHubPRO
Section Overview
Q3

Question 3 of 50

Part 1: Nuxt 3 Fundamentals (Junior / Entry-Level)

6%
Nuxt & Universal SSR (50 Qs)•Part 1: Nuxt 3 Fundamentals (Junior / Entry-Level)

How does file-based routing work in Nuxt 3?

mid LevelCode ExampleRoutingVue & Nuxt

In Simple Words: Every .vue file inside the pages/ directory automatically maps to a corresponding URL path.

Routing Patterns:

  1. Static Routes: pages/contact.vue -> /contact
  2. Index Routes: pages/index.vue -> /, pages/users/index.vue -> /users
  3. Dynamic Parameters: pages/users/[id].vue -> /users/:id (accessed via useRoute().params.id).
  4. Catch-All / 404 Routes: pages/[...slug].vue -> matches all unmatched routes (e.g. /docs/getting-started/setup).
  5. Nested Routes: pages/parent.vue containing <NuxtPage /> with pages/parent/child.vue.

Practical Code Example:

vue
<!-- pages/products/[id].vue -->
<script setup lang="ts">
const route = useRoute();
const productId = route.params.id; // Type-safe route parameter
</script>

<template>
  <div>
    <h1>Product Details: {{ productId }}</h1>
  </div>
</template>