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.
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.
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.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.
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.
Continue learning about more advanced topics in Nuxt.js, such as dynamic routes, async data, and how to integrate with APIs.
about.vue
in the pages
directory and display some text on it.about.vue
page.about.vue
page.about.vue
page:<template>
<div>
<h1>About Us</h1>
<p>We are a team of Nuxt.js enthusiasts.</p>
</div>
</template>
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>
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>
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!