Introduction to Nuxt & Vue.js: Getting Started

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

In this first of a series of posts, I will introduce you to Nuxt.js – an open-source framework that makes web development simple and powerful with Vue.js.
I’ve recently been writing about Next.js where we created a simple app to try out the main features, let’s do the same with Nuxt.js and Vue.js.
I used the nuxi@latest init command to create the default app structure.
pnpx nuxi@latest init daily-top-3-nuxt
We do the usual install and start-up commands:
pnpm install
pnpm dev
This gives the bare bones app page:

Great, everything is running. Let's get some data on the page.
For the database, we'll be using Postgres in Docker and Prisma to access the data. I won't go into detail here, but you can check out my previous post on Step-by-Step: Setting Up a Postgres Database with Docker and Seeding Test Data.
We can now start the database up with docker-compose up.
Install:
pnpm install prisma @prisma/client --save-dev
pnpx prisma init
Update the generated .env file in the project route with:
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres"
Then update the Prisma schema:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model top3 {
id Int @id @default(autoincrement())
created_at DateTime @default(now()) @db.Date
todos todo[]
}
model todo {
id Int @id @default(autoincrement())
title String
complete Boolean @default(false)
top3 top3 @relation(fields: [top3_id], references: [id], onDelete: Cascade)
top3_id Int
}
Generate the typesafe Prisma Client with:
pnpx prisma generate
We'll create a GET API at the url /api/top3. Create a file named top3.get.ts inside the server/api directory. This file will handle any GET requests to /api/top3.
// server/api/top3.get.ts
import {getTop3} from "~/lib/prisma";
export default defineEventHandler(async () => {
return await getTop3();
})
In the App.vue page we fetch and display the data using useAsyncData and v-for to loop through the data:
<script setup>
const {data} = await useAsyncData('count', () => $fetch('/api/top3'))
</script>
<template>
<div>
<h1>Top 3</h1>
<ol>
<li v-for="todo in data.todos">{{ todo.title }}</li>
</ol>
</div>
</template>
This renders our data to the page:

Congratulations! You have successfully learned how to fetch and display data from a database using Nuxt.js. You have also seen how to use asyncData and v-for directives to handle asynchronous data and render lists. In the next post, we will explore how to add and update data using forms and API methods.
You can find the complete code for this tutorial on Github.