Applying Layouts in Nuxt.js

Tutorial 3 of 5

Applying Layouts in Nuxt.js

1. Introduction

In this tutorial, we will learn how to apply layouts to different pages in a Nuxt.js application. Layouts are a powerful feature in Nuxt.js that allows you to design a page layout and use it across multiple pages.

By the end of this tutorial, you will understand:
- What layouts are and how they work in Nuxt.js
- How to create a layout
- How to apply a layout to a page

Prerequisites:
- Basic understanding of Vue.js
- Familiarity with JavaScript and ES6 syntax
- Some knowledge of Nuxt.js would be beneficial

2. Step-by-Step Guide

In Nuxt.js, layouts are stored in the layouts directory of your project. By default, Nuxt.js includes a default.vue layout.

To create a new layout, simply create a new .vue file in the layouts directory. The name of the file will be the name of the layout.

To use a layout in a page, you need to add a layout property in the page's component export and set it to the name of the layout.

Best Practices and Tips

  • Keep layouts as simple as possible. They are meant to be a structural shell for your pages.
  • Remember to add a <nuxt /> component in your layout. This is where the page content will be injected.

3. Code Examples

Example 1: Creating a Layout

Let's create a layout called blog.vue in the layouts directory.

<template>
  <div>
    <header>
      <h1>My Blog</h1>
    </header>
    <nuxt />
  </div>
</template>

In this example, our layout consists of a header with the title "My Blog", and a <nuxt /> component where the page content will be injected.

Example 2: Using a Layout

To use our blog layout, we add the layout property to a page's component export.

In pages/about.vue:

<template>
  <div>About us</div>
</template>

<script>
export default {
  layout: 'blog'
}
</script>

In this example, the about page will use the blog layout. When rendered, it will display the "My Blog" header and the "About us" content.

4. Summary

In this tutorial, we learned how to create and use layouts in Nuxt.js. We created a blog layout and applied it to an about page.

Next steps for learning would be to explore more complex layouts and to learn how to use nested layouts.

5. Practice Exercises

Exercise 1: Create a main layout with a navigation bar and a footer. Apply this layout to a home page.

Exercise 2: Create a post layout with a title and a back button. Apply this layout to a post page.

Exercise 3: Create a user layout with a user profile section. Apply this layout to a profile page.

Solutions:

  • The main layout should have a <nav> and <footer> elements, and a <nuxt /> component.
  • The post layout should have a <h1> for the title and a back button, and a <nuxt /> component.
  • The user layout should have a user profile section and a <nuxt /> component.
  • Each page should have a layout property set to the name of the layout.

Tips: Remember to keep your layouts simple and structural. The <nuxt /> component is where the page content will be injected.