What is Nuxt

Learn Nuxt in Y minutes

Features

  • Nuxt is full-featured Full-stack meta framework based on Vue 3. For an up-to-date list of features, please visit Nuxt.com
  • Front-end with Vue
  • Back-end with Nitro

Install

npx nuxi@latest init <project-name>
# or yarn, pnpm, bun
# Node 18+ required

Config settings (selected)

// nuxt.config.ts

export default defineNuxtConfig({
  ssr: true, // (or false) if the page should be rendered on the server or not, for SPA, use false
  extends: [' ... '], // Nuxt Layers, feature to include other Nuxt projects, sources can be local, GitHub, npm, etc, more info at https://nuxt.com/docs/getting-started/layers
  modules: ['@nuxt/eslint'], // list of Nuxt modules, @nuxt are maintained by the team, @nuxtjs are maintained by the community, full list at https://nuxt.com/modules
  $development: { }, // or $production - env variable overrides
  devTools: { enabled: true }, // turn Nuxt DevTool on/off
  runtimeConfig: { // use with 'const runtimeConfig = useRuntimeConfig()' in the component
    apiSecret: '123',
    public: {
      apiBase: '/api'
    }
  },
  vite: {}, // by default Nuxt uses Vue as dev/build tool OR
  webpack: {},
})

// app.config.ts
export default defineAppConfig({
  title: 'My Title'
})

// runtimeConfig: Private or public tokens that need to be specified after build using environment variables.
// app.config: Public tokens that are determined at build time, website configuration such as theme variant, title and any project config that are not sensitive.

Views Structure

App

  • app.vue - entry file for Nuxt project
<template>
  <div>
    <NuxtLayout>
      <NuxtPage />
    </NuxtLayout>
  </div>
</template>

Layouts

  • /layouts/default.vue - entry file for Nuxt project
<template>
  <div>
    <slot /> // where all the page content will be rendered
  </div>
</template>

Pages - Nuxt uses powerfull file-based paging system or routing and rendering content

  • requres that /pages folder is created
  • /pages/index.vue - will render at mywebsite.com on the landing page
  • /pages/about.vue - mywebsite.com/about

Assets

  • /public - will server content as-is
  • /assets - will be processed by build tools (Vite or Webpack)
  • /posts/id.vue - post Id can be accesed with useRoute().params.id inside .vue files

Route Middleware

Will execute before entering or leaving pages

  • inline
<script setup lang="ts">
definePageMeta({
  middleware: [
    function (to, from) {
      // Custom inline middleware
    },
    'auth',
  ],
})
</script>
  • /middleware folder, then used inside .vue file
  • /middleware/auth.ts
export default defineNuxtRouteMiddleware((to, from) => {
  // isAuthenticated() is an example method verifying if a user is authenticated
  if (isAuthenticated() === false) {
    return navigateTo('/login')
  }
})
<script setup lang="ts">
definePageMeta({
  middleware: 'auth'
})
</script>
Copyright © 2024