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:
- Static Routes:
pages/contact.vue->/contact - Index Routes:
pages/index.vue->/,pages/users/index.vue->/users - Dynamic Parameters:
pages/users/[id].vue->/users/:id(accessed viauseRoute().params.id). - Catch-All / 404 Routes:
pages/[...slug].vue-> matches all unmatched routes (e.g./docs/getting-started/setup). - Nested Routes:
pages/parent.vuecontaining<NuxtPage />withpages/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>