Skip to content

Nuxt.js Cheatsheet

Vue framework for modern web applications with SSR and SSG.

01

Getting Started

Page

File-based routing in pages/.

nuxtjs
<!-- pages/index.vue -->
<template>
  <h1>Hello</h1>
</template>

Data Fetching

useFetch for data fetching with SSR support.

nuxtjs
<script setup>
const { data } = await useFetch("/api/data");
</script>

<template>
  <div>{{ data }}</div>
</template>

Layouts

Layouts use slots for page content.

nuxtjs
<!-- layouts/default.vue -->
<template>
  <header>Nav</header>
  <main><slot /></main>
</template>
02

Features

Dynamic Routes

[slug] for dynamic routes.

nuxtjs
<!-- pages/blog/[slug].vue -->
<script setup>
const route = useRoute();
</script>

<template>
  <h1>{{ route.params.slug }}</h1>
</template>

Composables

Auto-imported composables.

nuxtjs
// composables/useCounter.ts
export function useCounter() {
  const count = ref(0);
  const inc = () => count.value++;
  return { count, inc };
}

Middleware

Route middleware.

nuxtjs
// middleware/auth.ts
export default defineNuxtRouteMiddleware((to) => {
  if (!isAuthenticated()) return navigateTo("/login");
});

Was this helpful?