Skip to content

Kapitel 16: Erweiterte Lernswege

In diesem Kapitel entdecken wir erweiterte Themen und Modul-Ökosystem von Nuxt 3.


16.1 Nuxt-Modul-Ökosystem

Nuxt 3 hat ein reichhaltiges Ökosystem von offiziellen und Community-Modulen.

Wichtigste offizielle Module:

ModulBeschreibungInstallation
@nuxt/contentMarkdown-Inhalte verwaltenpnpm add @nuxt/content
@nuxt/imageBild-Optimierungpnpm add @nuxt/image
@nuxtjs/tailwindcssTailwind CSS integrierenpnpm add @nuxtjs/tailwindcss
@nuxtjs/axiosAxios integrierenpnpm add @nuxtjs/axios
@pinia/nuxtPinia State Managementpnpm add @pinia/nuxt
@nuxtjs/sitemapSitemap generierenpnpm add @nuxtjs/sitemap
@nuxtjs/robotsrobots.txt verwaltenpnpm add @nuxtjs/robots

Beispiel: @nuxt/content verwenden

Installation:

bash
pnpm add @nuxt/content

In nuxt.config.ts konfigurieren:

typescript
export default defineNuxtConfig({
  modules: [
    '@nuxt/content'
  ]
})

Inhalt erstellen (content/blog/mein-erster-beitrag.md):

markdown
---
title: 'Mein erster Beitrag'
description: 'Dies ist ein Blog-Beitrag mit Nuxt Content'
date: 2024-01-15
---

# Willkommen!

Dies ist der Inhalt meines Beitrags...

Inhalt anzeigen (pages/blog/[slug].vue):

vue
<script setup>
const route = useRoute()
const { data: post } = await useAsyncData(
  `post-${route.params.slug}`,
  () => queryContent(`/blog/${route.params.slug}`).findOne()
)
</script>

<template>
  <div class="post-page">
    <h1>{{ post.title }}</h1>
    <ContentRenderer :value="post" />
  </div>
</template>

16.2 Mobile-Anpassung (Mobile-First)

Nuxt 3 ist perfekt für mobile-optimierte Websites.

Option 1: Vant (Vue Mobile UI)

Installation:

bash
pnpm add vant @vant/nuxt

Konfiguration (nuxt.config.ts):

typescript
export default defineNuxtConfig({
  modules: [
    '@vant/nuxt'
  ],
  vant: {
    // Automatische Import von Vant-Komponenten
  }
})

Verwendung:

vue
<template>
  <van-button type="primary">Primary Button</van-button>
  <van-cell-group>
    <van-cell title="Zelle 1" value="Inhalt" />
    <van-cell title="Zelle 2" value="Inhalt" />
  </van-cell-group>
</template>

Option 2: UnoCSS (Schnelle CSS-Erstellung)

Installation:

bash
pnpm add -D unocss @unocss/nuxt

Konfiguration:

typescript
export default defineNuxtConfig({
  modules: [
    '@unocss/nuxt'
  ]
})

Verwendung:

vue
<template>
  <div class="min-h-screen bg-gray-100 flex items-center justify-center">
    <div class="bg-white p-8 rounded-lg shadow-md">
      <h1 class="text-2xl font-bold mb-4">UnoCSS</h1>
      <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
        Button
      </button>
    </div>
  </div>
</template>

16.3 TypeScript mit Nuxt 3

Nuxt 3 ist TypeScript-first!

TypeScript in Nuxt 3 aktivieren:

tsconfig.json (wird automatisch generiert):

json
{
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "strict": true
  }
}

Typisierte Composables:

typescript
// composables/usePosts.ts
interface Post {
  id: number
  title: string
  content: string
}

export const usePosts = () => {
  const posts = ref<Post[]>([])

  const fetchPosts = async () => {
    const data = await $fetch<Post[]>('/api/posts')
    posts.value = data
  }

  return {
    posts: readonly(posts),
    fetchPosts
  }
}

Typisierte API-Routen:

typescript
// server/api/posts.ts
import { defineEventHandler } from 'h3'

interface Post {
  id: number
  title: string
}

export default defineEventHandler(async (event): Promise<Post[]> => {
  // Typisierte Rückgabe
  return [
    { id: 1, title: 'Beitrag 1' }
  ]
})

16.4 Server-Side Entwicklung (API-Routen)

Nuxt 3 ermöglicht es Ihnen, Backend-APIs direkt in Ihrem Nuxt-Projekt zu erstellen!

API-Route erstellen (server/api/hello.ts):

typescript
export default defineEventHandler((event) => {
  return {
    message: 'Hallo von der API!',
    timestamp: new Date().toISOString()
  }
})

Zugriff: /api/hello


Datenbank-Anbindung (Beispiel mit Prisma):

Installation:

bash
pnpm add @prisma/client @prisma/nuxt
pnpm add -D prisma

server/api/users.ts:

typescript
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export default defineEventHandler(async (event) => {
  const users = await prisma.user.findMany()
  return users
})

16.5 Zusammenfassung

In diesem Kapitel haben Sie gelernt:

  • ✅ Das Nuxt-Modul-Ökosystem zu nutzen (@nuxt/content, @nuxt/image)
  • ✅ Mobile-Anpassung mit Vant oder UnoCSS
  • ✅ TypeScript in Nuxt 3 zu verwenden (typisierte Composables)
  • ✅ Server-Side Entwicklung (API-Routen, Datenbank-Anbindung)

Nächste Schritte: Im letzten Kapitel stellen wir Lernressourcen zusammen, um Ihr Nuxt 3 Wissen zu vertiefen.


📚 Weiterführende Ressourcen


Nächstes Kapitel: Kapitel 17: Lernressourcen →

Frei für alle Anfänger