Understanding Nuxt.js project structure

Tutorial 4 of 5

Understanding Nuxt.js Project Structure

1. Introduction

Goal

The goal of this tutorial is to aid you in understanding the structure of a Nuxt.js project. Nuxt.js is a robust framework for creating Vue.js applications, and knowing its structure will allow you to navigate and maintain your application effectively.

Learning Outcomes

By the end of this tutorial, you will have a solid understanding of how files and directories are organized within a Nuxt.js project and why they are structured that way.

Prerequisites

  • Basic knowledge of JavaScript and Vue.js
  • Installed Node.js and NPM on your machine
  • Basic understanding of how to use the terminal or command line

2. Step-by-Step Guide

Nuxt.js has a distinct project structure. The main directories and files you'll encounter include:

  • assets: This directory contains un-compiled assets such as Less, Sass, or JavaScript.
  • components: Vue components go here.
  • layouts: This directory includes your application layouts.
  • middleware: Middleware lets you define custom functions that run before rendering either a page or a group of pages.
  • pages: Every Vue file in this directory becomes a route automatically.
  • plugins: This is for JavaScript plugins that you want to run before instantiating the root Vue.js application.
  • static: Files in this directory are directly mapped to the server root.
  • store: This directory contains your Vuex Store files.

Best Practices and Tips

  • Keep business logic inside your Vuex store.
  • Use the middleware for things like authentication checks.
  • Keep your components as small as possible. If they start getting large, consider breaking them down into smaller sub-components.

3. Code Examples

Here is a basic example of a Nuxt.js project structure:

-| assets
-| components
-| layouts
-| middleware
-| pages
-| plugins
-| static
-| store

Let's say you have a page named index.vue inside your pages directory. It may look something like this:

<template>
<div>
    <h1>{{ title }}</h1>
    <p>{{ description }}</p>
</div>
</template>

<script>
export default {
  data() {
    return {
      title: 'Hello, Nuxt.js!',
      description: 'This is an example page.'
    }
  }
}
</script>

This page will be served when you navigate to the root (/) of your application. The title and description data properties will be rendered on the page.

4. Summary

You've learned about the structure of a Nuxt.js project, including the purpose of each directory. You've also seen an example of how to create a simple page.

Next Steps

Continue learning about more advanced topics in Nuxt.js, such as dynamic routes, async data, and how to integrate with APIs.

Additional Resources

5. Practice Exercises

  1. Creating a New Page: Create a new page called about.vue in the pages directory and display some text on it.
  2. Using Components: Create a Vue component and use it on your about.vue page.
  3. Using the Store: Create a Vuex store file, add some data to the state, and display it on your about.vue page.

Solutions

  1. Here is a simple about.vue page:
<template>
<div>
    <h1>About Us</h1>
    <p>We are a team of Nuxt.js enthusiasts.</p>
</div>
</template>
  1. Create a component called MyComponent.vue in the components directory:
<template>
<div>
    <h1>Hello from MyComponent</h1>
</div>
</template>

Then, use it in your about.vue page:

<template>
<div>
    <h1>About Us</h1>
    <p>We are a team of Nuxt.js enthusiasts.</p>
    <MyComponent />
</div>
</template>

<script>
import MyComponent from '~/components/MyComponent'

export default {
  components: {
    MyComponent
  }
}
</script>
  1. Create a index.js file in the store directory:
export const state = () => ({
  team: ['Alice', 'Bob', 'Charlie']
})

Then, display the team members in your about.vue page:

<template>
<div>
    <h1>About Us</h1>
    <p>We are a team of Nuxt.js enthusiasts:</p>
    <ul>
      <li v-for="member in team" :key="member">{{ member }}</li>
    </ul>
</div>
</template>

<script>
export default {
  computed: {
    team() {
      return this.$store.state.team
    }
  }
}
</script>

Further Practice

Try creating more pages, using more components, and adding more data to your Vuex store. Remember, practice is key to becoming proficient at anything, including Nuxt.js!