Building the UI: Views, Layouts, and Components
6 min read
Building the UI: Views, Layouts, and Components 🏗️
Welcome to the UI construction zone! In this section, we'll be laying the foundation for your Nuxt application's user interface. We'll explore how to structure your views, create reusable components, and maintain a consistent look and feel with layouts. Think of it as being the architect and interior designer of your web application all at once. Let's get our hands dirty and build something beautiful and functional.
1. App.vue vs the Pages Directory 📂
The Definition:
app.vue: This is the main, root component of your Nuxt application. Every single thing the user sees is ultimately rendered within this file.pagesdirectory: This special directory in your project enables file-system-based routing. In simple terms, you create.vuefiles here, and Nuxt automatically generates the corresponding web pages (routes) for them.
The Problem:
Imagine you're building a house. You need a main foundation and structure (app.vue) that holds everything together. But you also need individual rooms (a living room, a bedroom, a kitchen) that people can navigate between. How do you define this overall structure while also creating distinct, navigable areas?
The Solution:
Nuxt provides a clear separation of concerns with app.vue and the pages directory.
app.vueas the Blueprint: Useapp.vueto define the global shell of your application. This is the perfect place for elements that should appear on every single page, like a persistent header, a footer, or maybe a notification system. If you don't have apages/directory, Nuxt will treat your application as a single-page application whereapp.vueis the only view.App.vue Example:
vue<template> <div> <header>My Awesome App</header> <NuxtPage /> <!-- This is where your page content will be injected --> <footer>Copyright 2026</footer> </div> </template>In this example, the
<header>and<footer>will be present on every page, and the content from yourpagesdirectory will be rendered where<NuxtPage />is.pagesfor Individual Rooms: Thepagesdirectory is where you create the individual "rooms" or pages of your application. The structure of this directory directly maps to the URL structure of your site. For instance:pages/index.vuebecomes the homepage (/)pages/about.vuebecomes the about page (/about)pages/products/index.vuebecomes the products listing page (/products)
Pages Example (
pages/about.vue):vue<template> <main> <h1>About Us</h1> <p>This is the about page content.</p> </main> </template>When a user navigates to
/about, the content ofabout.vuewill be rendered inside the<NuxtPage />component inapp.vue.
Interesting Fact: If you don't create a pages directory, Nuxt won't include the vue-router dependency, keeping your application's bundle size smaller. This is great for simple, single-page sites or landing pages.
2. Creating Reusable Components ♻️
The Definition:
Components are self-contained, reusable pieces of your user interface. Think of them as custom HTML elements that you can use throughout your application.
The Problem:
You find yourself writing the same block of code for a button with specific styling and functionality on multiple pages. If you need to change the button's color, you have to go and edit it in every single place. This is tedious, error-prone, and a violation of the DRY (Don't Repeat Yourself) principle.
The Solution:
Create a reusable component! In Nuxt, you can create a components directory in your project's root. Any .vue file you place in this directory will be automatically available to use in your pages and other components without needing to import them.
Example: Creating a Reusable Button
Create the component file:
components/PrimaryButton.vuevue<template> <button class="primary-button"> <slot /> <!-- This allows you to pass text or other elements into the button --> </button> </template> <style scoped> .primary-button { background-color: #00DC82; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; } </style>Use the component in a page:
pages/index.vuevue<template> <div> <h1>Welcome!</h1> <PrimaryButton>Click Me!</PrimaryButton> </div> </template>
Now, if you need to change the button's appearance, you only have to edit PrimaryButton.vue, and the changes will be reflected everywhere it's used.
Funny Exaggeration: Imagine if every time you wanted to use a screw, you had to forge it from raw metal right there on the spot. That's what it's like to not use components. With components, you have a well-organized toolbox of ready-to-use screws for any situation.
3. Using Layouts for Consistent Page Structure 🖼️
The Definition:
Layouts are essentially wrappers for your pages. They allow you to define a common structure, like a header and footer, that can be applied to multiple pages.
The Problem:
Your website has several pages that should all share the same header and footer. While you could include the header and footer components on every single page, this again violates the DRY principle. What if you need a group of pages to have a different common structure?
The Solution:
Nuxt's layouts directory to the rescue! By creating a layouts folder, you can define different page structures.
Creating a Default Layout:
A file named layouts/default.vue will be used as the default layout for all your pages.
layouts/default.vue Example:
<template>
<div>
<AppHeader />
<main>
<slot /> <!-- Page content goes here -->
</main>
<AppFooter />
</div>
</template>With this in place, every page in your pages directory will automatically be wrapped with this layout, giving them a consistent header and footer.
4. Dynamic and Custom Layouts 🎨
The Definition:
Dynamic and custom layouts allow you to specify different layouts for different pages.
The Problem:
Your application has a public-facing area and a private dashboard for authenticated users. The public pages need a simple layout with a main navigation, while the dashboard requires a more complex layout with a sidebar and a user-specific header.
The Solution:
You can create multiple layout files in the layouts directory and then specify which layout a particular page should use.
Creating a Custom Layout:
Create the custom layout file:
layouts/dashboard.vuevue<template> <div class="dashboard-layout"> <DashboardSidebar /> <div class="main-content"> <DashboardHeader /> <slot /> <!-- Page content --> </div> </div> </template> <style scoped> .dashboard-layout { display: flex; } .main-content { flex-grow: 1; } </style>Apply the custom layout to a page:
pages/dashboard.vueYou can use the
definePageMetamacro to specify the layout.vue<template> <div> <h1>Welcome to your Dashboard!</h1> </div> </template> <script setup> definePageMeta({ layout: 'dashboard', }); </script>
Now, when a user navigates to the /dashboard page, it will use the dashboard.vue layout instead of the default.vue layout.
Hands-on Tip: When naming your layouts, use descriptive names like default, auth, or admin to make it clear what their purpose is. This will save you and your team a lot of head-scratching down the line.
Extra Reading & References:
- Nuxt Directory Structure: https://nuxt.com/docs/guide/directory-structure/nuxt
- App.vue: https://nuxt.com/docs/guide/directory-structure/app
- Pages Directory: https://nuxt.com/docs/guide/directory-structure/pages
- Components in Nuxt: https://nuxt.com/docs/guide/directory-structure/components
- Layouts: https://nuxt.com/docs/guide/directory-structure/layouts
Challenge
You are tasked with building the structural foundation for a Nuxt.js application that has both public-facing pages and a private admin area, each with a distinct layout.
Requirements:
Root Component (
app.vue):- Set up the main application entry point to render page components.
Layouts (
layouts/):- Create a
default.vuelayout. This layout must contain a<header>, a<footer>, and a content area for the page. - Create an
admin.vuelayout. This layout must have a<aside>element for a sidebar and a<main>element for the page content.
- Create a
Pages (
pages/):- Create a homepage at
pages/index.vue. - Create an
aboutpage atpages/about.vue. - Create a nested admin dashboard page at
pages/admin/dashboard.vue. - Configure the
dashboard.vuepage to explicitly use theadminlayout. The other pages should implicitly use thedefaultlayout.
- Create a homepage at
Component (
components/):- Create a reusable component named
UserProfileCard.vue. - This component must accept a
usernameprop of typeString. - Inside the
admin/dashboard.vuepage, use theUserProfileCardcomponent and pass the string "administrator" to itsusernameprop.
- Create a reusable component named
Expected result
Your final code should consist of seven distinct blocks of code, each corresponding to a specific file in a Nuxt.js application structure.
app.vue: Should contain only the<NuxtPage />component.layouts/default.vue: Should have a template with a<header>,<slot />, and<footer>.layouts/admin.vue: Should have a template with an<aside>element for a sidebar and a<main>element containing the<slot />.pages/index.vue: Should contain a simple template, for example,<h1>Welcome to the Homepage</h1>.pages/about.vue: Should contain a simple template, for example,<h1>About Us</h1>.pages/admin/dashboard.vue: Must contain a<script setup>block callingdefinePageMeta({ layout: 'admin' }). The template must render the<UserProfileCard username="administrator" />component.components/UserProfileCard.vue: Must have a<script setup>block usingdefinePropsto declare theusernameprop. The template should display the value of theusernameprop, for example, in a<div>or<p>tag.