Vue.js course Β· Module 15: TypeScript & Nuxt

Nuxt 3 - Basics

3 min read Β·
In this lesson8

Nuxt 3 is a meta-framework built on Vue 3 that provides SSR, SSG, file-based routing, and much more - out of the box, with no configuration. Think of it as the autopilot system of the NOVA LAB station - everything is configured and ready to go.

Creating a Nuxt 3 Project

npx nuxi@latest init nova-lab-app
cd nova-lab-app
npm install
npm run dev

File-Based Routing

In Nuxt 3, you don't configure the router manually. The file structure in the pages/ folder automatically creates routes:

pages/
β”œβ”€β”€ index.vue          β†’  /
β”œβ”€β”€ about.vue          β†’  /about
β”œβ”€β”€ crew/
β”‚   β”œβ”€β”€ index.vue      β†’  /crew
β”‚   └── [id].vue       β†’  /crew/123
└── modules/
    └── [...slug].vue  β†’  /modules/any/nested/path

The index.vue file is the main page of a given directory. Files with square brackets [id].vue are dynamic parameters.

Layouts

Layouts define the shared structure of pages (navigation, footer):

<!-- layouts/default.vue -->
<template>
  <div class="station">
    <nav class="nav">
      <NuxtLink to="/">Home</NuxtLink>
      <NuxtLink to="/about">About</NuxtLink>
      <NuxtLink to="/crew">Crew</NuxtLink>
    </nav>

    <main>
      <slot />
    </main>

    <footer>NOVA LAB &copy; 2087</footer>
  </div>
</template>

Pages automatically use the default layout. You can change the layout on a specific page:

<!-- pages/dashboard.vue -->
<script setup lang="ts">
definePageMeta({
  layout: 'admin'
})
</script>

useFetch - Data Fetching

useFetch is a Nuxt composable for fetching data. It works both on the server (SSR) and on the client:

<script setup lang="ts">
interface CrewMember {
  id: number
  name: string
  role: string
}

// useFetch works on server and client
const { data: crew, pending, error } = await useFetch<CrewMember[]>(
  '/api/crew'
)
</script>

<template>
  <div v-if="pending">Loading...</div>
  <div v-else-if="error">Error: {{ error.message }}</div>
  <ul v-else>
    <li v-for="member in crew" :key="member.id">
      {{ member.name }} - {{ member.role }}
    </li>
  </ul>
</template>

useFetch automatically:

  • Executes the request on the server during SSR
  • Serializes the data and sends it to the client (avoids double fetching)
  • Provides pending, error, data, refresh states

useAsyncData - More Flexible

useAsyncData gives full control over the data fetching logic:

<script setup lang="ts">
const { data: stats } = await useAsyncData('station-stats', () => {
  return $fetch('/api/stats')
})

// With data transformation
const { data: modules } = await useAsyncData(
  'modules',
  () => $fetch('/api/modules'),
  {
    transform: (data) => data.filter(m => m.active),
    watch: [selectedSector]  // automatic refresh
  }
)
</script>

Auto-imports

Nuxt 3 automatically imports:

  • Vue composables (ref, computed, watch...)
  • Nuxt composables (useFetch, useRoute, useState...)
  • Components from components/
  • Utilities from utils/ and composables/

No need to write import { ref } from 'vue' - Nuxt does it for us!

<script setup lang="ts">
// No imports! Nuxt auto-imports ref, computed, useFetch
const count = ref(0)
const doubled = computed(() => count.value * 2)

const { data } = await useFetch('/api/data')
</script>

Nuxt 3 Project Structure

nova-lab-app/
β”œβ”€β”€ pages/          # File-based routing
β”œβ”€β”€ components/     # Auto-imported components
β”œβ”€β”€ composables/    # Auto-imported composables
β”œβ”€β”€ layouts/        # Page layouts
β”œβ”€β”€ server/         # API endpoints (Nitro)
β”‚   └── api/        # /api/* routes
β”œβ”€β”€ public/         # Static files
β”œβ”€β”€ app.vue         # Root component
└── nuxt.config.ts  # Nuxt configuration

Server API (Nitro)

Nuxt 3 uses Nitro to handle API endpoints:

// server/api/crew.ts
export default defineEventHandler((event) => {
  return [
    { id: 1, name: 'Dr. Nova', role: 'Commander' },
    { id: 2, name: 'Atlas', role: 'Navigator' },
    { id: 3, name: 'Echo', role: 'Engineer' }
  ]
})
Code for this lesson: App.vue
<script setup lang="ts">
import { ref } from 'vue'

// Simulation of the Nuxt 3 structure
// (Sandpack does not support Nuxt, so we simulate the concept)

type Page = 'home' | 'crew' | 'modules' | 'crew-detail'

const currentPage = ref<Page>('home')
const selectedCrewId = ref<number | null>(null)

// Simulation of useFetch
interface CrewMember {
  id: number
  name: string
  role: string
}

interface StationModule {
  id: string
  name: string
  status: string
}

// Data "from the API"
const crewData: CrewMember[] = [
  { id: 1, name: 'Dr. Nova', role: 'Commander' },
  { id: 2, name: 'Atlas', role: 'Navigator' },
  { id: 3, name: 'Echo', role: 'Engineer' },
  { id: 4, name: 'Vega', role: 'Scientist' }
]

const modulesData: StationModule[] = [
  { id: 'm1', name: 'Life Support', status: 'online' },
  { id: 'm2', name: 'Navigation', status: 'online' },
  { id: 'm3', name: 'Science Lab', status: 'maintenance' }
]

// Simulation of loading state
const pending = ref(false)

function navigate(page: Page, id?: number) {
  pending.value = true
  setTimeout(() => {
    currentPage.value = page
    if (id) selectedCrewId.value = id
    pending.value = false
  }, 300)
}

function getCrewMember(id: number) {
  return crewData.find(c => c.id === id)
}
</script>

<template>
  <div class="nova-lab">
    <nav class="nav">
      <button @click="navigate('home')" :class="{ active: currentPage === 'home' }">Home</button>
      <button @click="navigate('crew')" :class="{ active: currentPage === 'crew' }">Crew</button>
      <button @click="navigate('modules')" :class="{ active: currentPage === 'modules' }">Modules</button>
    </nav>

    <div v-if="pending" class="loading">Loading...</div>

    <div v-else>
      <!-- pages/index.vue -->
      <div v-if="currentPage === 'home'" class="page">
        <h1>NOVA LAB - Nuxt 3 Demo</h1>
        <p>Simulation of file-based routing and useFetch</p>
        <div class="info-box">
          <p><strong>pages/</strong> index.vue -> /</p>
          <p><strong>pages/</strong> crew/index.vue -> /crew</p>
          <p><strong>pages/</strong> crew/[id].vue -> /crew/:id</p>
          <p><strong>pages/</strong> modules.vue -> /modules</p>
        </div>
      </div>

      <!-- pages/crew/index.vue -->
      <div v-if="currentPage === 'crew'" class="page">
        <h1>Crew</h1>
        <p class="note">Data fetched via useFetch('/api/crew')</p>
        <div
          v-for="member in crewData"
          :key="member.id"
          class="card clickable"
          @click="navigate('crew-detail', member.id)"
        >
          <strong>{{ member.name }}</strong>
          <span>{{ member.role }}</span>
        </div>
      </div>

      <!-- pages/crew/[id].vue -->
      <div v-if="currentPage === 'crew-detail' && selectedCrewId" class="page">
        <button @click="navigate('crew')" class="back-btn">Back</button>
        <h1>{{ getCrewMember(selectedCrewId)?.name }}</h1>
        <p>Role: {{ getCrewMember(selectedCrewId)?.role }}</p>
        <p class="note">Route: /crew/{{ selectedCrewId }}</p>
      </div>

      <!-- pages/modules.vue -->
      <div v-if="currentPage === 'modules'" class="page">
        <h1>Station modules</h1>
        <p class="note">useAsyncData('modules', () => $fetch('/api/modules'))</p>
        <div v-for="mod in modulesData" :key="mod.id" class="card">
          <strong>{{ mod.name }}</strong>
          <span :class="mod.status">{{ mod.status }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.nova-lab {
  background: #0a0e27;
  color: #00ff88;
  min-height: 100vh;
  padding: 20px;
  font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 10px; }
.nav {
  display: flex;
  gap: 8px;
  margin-bottom: 15px;
  border-bottom: 1px solid rgba(0,180,216,0.3);
  padding-bottom: 12px;
}
.nav button {
  background: rgba(0,180,216,0.1);
  color: #00b4d8;
  border: 1px solid #00b4d8;
  padding: 8px 18px;
  border-radius: 6px;
  cursor: pointer;
  font-family: monospace;
}
.nav button.active { background: #00b4d8; color: white; }
.page { padding: 10px 0; }
.card {
  display: flex;
  justify-content: space-between;
  background: rgba(0,180,216,0.08);
  border: 1px solid #00b4d8;
  padding: 12px 16px;
  margin: 6px 0;
  border-radius: 8px;
}
.card.clickable { cursor: pointer; }
.card.clickable:hover { background: rgba(0,180,216,0.15); }
.note { color: #666; font-size: 12px; margin-bottom: 10px; }
.info-box {
  background: rgba(0,255,136,0.05);
  border: 1px solid rgba(0,255,136,0.2);
  padding: 14px;
  border-radius: 8px;
  margin-top: 10px;
  font-size: 13px;
}
.info-box p { margin: 4px 0; color: #ccc; }
.loading { color: #f39c12; text-align: center; padding: 40px; }
.back-btn {
  background: none;
  color: #00b4d8;
  border: 1px solid #00b4d8;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  margin-bottom: 10px;
  font-family: monospace;
}
.online { color: #00ff88; }
.maintenance { color: #f39c12; }
.offline { color: #e74c3c; }
</style>

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How does Nuxt 3 create application routing?

  2. 2. In which folder does Nuxt 3 place files that automatically become application routes?

These are 2 of 6 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Use onMounted to access window - an object unavailable on the server.

  • Horizontal ordering

    Arrange the file path for a dynamic route /crew/123 in Nuxt:

  • Vertical ordering

    Arrange the steps of Incremental Static Regeneration:

  • Click in order

    Arrange useFetch states in the order of a typical flow:

  • Horizontal ordering

    Arrange the Nuxt layout structure with the NuxtLink component:

  • Vertical ordering

    Arrange strategies from best SEO to weakest:

  • Click in order

    Arrange the commands to create and run a Nuxt 3 project:

  • Vertical ordering

    Arrange TypeScript concepts in Vue from simplest to most advanced:

  • Vertical ordering

    Arrange the lifecycle of data fetched by useFetch in Nuxt SSR:

  • Horizontal ordering

    Arrange the name of Nuxt 3's main configuration file:

  • Code editor

    Define StationModule and Alert interfaces, type props, emits, and computed.

  1. QuestionFile-based Routing in Nuxt
  2. QuestionNuxt 3 Project Structure
  3. QuestionuseFetch in Nuxt 3
  4. Coding taskPractice: SSR-safe Code
  5. Put in orderDynamic Route in Nuxt
  6. QuestionAuto-imports in Nuxt 3
  7. Put in orderISR - Data Flow
  8. Click in orderuseFetch States
  9. QuestionWhen to Use SSR
  10. Put in orderLayouts in Nuxt 3
  11. Put in orderSSR vs SPA - SEO Differences
  12. Click in orderCreating a Nuxt 3 Project
  13. Put in orderVue Typing - from Basics to Advanced
  14. Put in orderuseFetch Lifecycle in Nuxt
  15. Put in orderNuxt Configuration File
  16. QuestionWindow in SSR
  17. Coding taskPractice: Typed Station Panel

Useful articles