Getting Started with Nuxt.js: A Beginner’s Guide
Nuxt.js is a powerful and flexible framework built on top of Vue.js that enables server-side rendering (SSR), static site generation (SSG), and single-page applications (SPA). It simplifies the process of creating production-ready applications and provides features like automatic routing, page components, and server-side rendering out of the box.
If you're new to Nuxt.js, this guide will walk you through the essential concepts to help you get started quickly.
What is Nuxt.js?
Nuxt.js is a higher-level framework that builds on Vue.js, enhancing it by providing tools and conventions for handling common tasks such as routing, server-side rendering, and file structure organization. By default, Nuxt.js sets up your project with best practices in mind, so you can focus more on building features rather than managing configurations.
Setting Up Your Nuxt.js Project
To start using Nuxt.js, you'll need to install it. If you already have Node.js and npm/yarn installed, follow these steps:
- Create a new Nuxt.js project:
Open your terminal and run the following command to create a new project:npx create-nuxt-app my-nuxt-project
This command will prompt you to choose a package manager (npm or yarn), a UI framework (like Vuetify or Bootstrap), and other settings. If you're unsure, you can go with the default options. - Navigate to your project directory:
Once the project is created, navigate to the project folder:cd my-nuxt-project
- Start the development server:
To start the development server, run:npm run dev
Your Nuxt.js app will now be running onhttp://localhost:3000
.
Understanding the Folder Structure
Nuxt.js uses a convention-over-configuration approach, meaning it follows certain file and folder structures to organize your application. Here are the key folders in a typical Nuxt.js project:
pages/
: This folder is where your route-based components go. Every.vue
file inside this folder automatically becomes a route in your application. For example,pages/index.vue
will become the homepage route (/
), andpages/about.vue
will become the/about
route.components/
: This is where you store your reusable components. Unlikepages/
, the components in this folder do not automatically map to a route but can be imported and used in pages or other components.layouts/
: Here, you define layouts that control the structure of your pages. For instance, a layout could define a header, footer, or sidebar that is consistent across multiple pages.store/
: This folder holds Vuex store modules if you’re managing state with Vuex. While not required, it’s useful for global state management.static/
: Thestatic/
folder is where you store files that you want to be served as static assets, such as images, fonts, or other public files.
Pages and Routing in Nuxt.js
One of the most convenient features of Nuxt.js is its automatic routing system. Each file you place in the pages/
directory corresponds to a route in your app. Here’s how it works:
pages/index.vue
: This file will automatically be routed to the homepage (/
).pages/about.vue
: This will automatically be routed to/about
.- Dynamic routes: You can create dynamic routes by using a file name like
[slug].vue
. For example,pages/blog/[slug].vue
will handle routes like/blog/my-first-post
.
Layouts in Nuxt.js
Layouts in Nuxt.js allow you to define a common structure for different types of pages. For instance, you might have a layout for the homepage and a different one for the admin dashboard.
To create a layout, add a .vue
file in the layouts/
directory, such as default.vue
or admin.vue
. Here’s an example of a simple layout:
<!-- layouts/default.vue -->
<template>
<div>
<header>
<nav>
<ul>
<li><NuxtLink to="/">Home</NuxtLink></li>
<li><NuxtLink to="/about">About</NuxtLink></li>
</ul>
</nav>
</header>
<main>
<Nuxt />
</main>
<footer>© 2025 My Nuxt App</footer>
</div>
</template>
In this layout, the <Nuxt />
component is where the content of each page will be injected.
To use a layout in a page, you can specify it using the layout
property:
<script>
export default {
layout: 'default'
}
</script>
Fetching Data in Nuxt.js
Nuxt.js provides a few methods for fetching data asynchronously before rendering your pages, which is particularly useful for server-side rendering.
asyncData
: This function is called before the page is rendered, allowing you to fetch data that needs to be displayed on the page.
Here’s an example of using asyncData
to fetch data for the page:
<script>
export default {
async asyncData() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return { data };
}
}
</script>
<template>
<div>
<h1>Data</h1>
<pre>{{ data }}</pre>
</div>
</template>
This will fetch the data before the page is rendered, ensuring that the data is available when the page is loaded.
Conclusion
Nuxt.js makes developing Vue.js applications easier by providing built-in features like automatic routing, server-side rendering, and powerful tools for managing your app. By using the structure and conventions that Nuxt.js offers, you can focus on building your app's functionality instead of spending time on configurations.
Now that you've learned some of the basics of Nuxt.js, you can start building your own full-fledged applications with ease. Whether you're creating a single-page app, a static site, or a server-side rendered application, Nuxt.js is a fantastic framework that simplifies the development process.