🎓 A1Learner
🔍
0 of 50 lessons completed

Capstone: A Notes App

A notes app lets a reader add notes, mark them done, and delete them. This capstone joins components, props, refs, lists, and forms in one app.

Add a note

Submit pushes a new note object onto the array.

notes.value.push({ id: nextId.value, text: newNote.value, done: false })

Draw the notes

Each note gets one NoteItem with its own key.

<NoteItem v-for="note in notes" :key="note.id" :note="note" @toggle="toggleNote(note.id)" @remove="removeNote(note.id)" />

The empty message

The line shows only when the list is empty.

<p v-if="notes.length === 0">No notes yet.</p>

Example

<script setup>
import { ref, defineComponent } from 'vue'
const NoteItem = defineComponent({
  props: ['note'],
  emits: ['toggle', 'remove'],
  template: `<li><span>{{ note.text }}</span><span v-if="note.done"> Done</span><button @click="$emit('toggle')">Done</button><button @click="$emit('remove')">Delete</button></li>`
})
const notes = ref([
  { id: 1, text: 'Learn ref', done: true },
  { id: 2, text: 'Build a list', done: false }
])
const newNote = ref('')
const nextId = ref(3)
function addNote() {
  if (newNote.value === '') {
    return
  }
  notes.value.push({ id: nextId.value, text: newNote.value, done: false })
  nextId.value++
  newNote.value = ''
}
function toggleNote(id) {
  const note = notes.value.find((item) => item.id === id)
  if (note) {
    note.done = !note.done
  }
}
function removeNote(id) {
  notes.value = notes.value.filter((item) => item.id !== id)
}
</script>
<template>
  <div>
    <h1>My Notes</h1>
    <form @submit.prevent="addNote">
      <input v-model="newNote" placeholder="New note" />
      <button>Add note</button>
    </form>
    <p v-if="notes.length === 0">No notes yet.</p>
    <ul>
      <NoteItem v-for="note in notes" :key="note.id" :note="note" @toggle="toggleNote(note.id)" @remove="removeNote(note.id)" />
    </ul>
  </div>
</template>

Key points

  • The notes live in a ref array of small objects with id, text, and done.
  • A form with v-model reads the new note text, and submit adds it to the array.
  • v-for draws one NoteItem per note, with :key set to the note id.
  • NoteItem takes its note through a prop and reports clicks through emits.
  • v-if shows a No notes yet line when the list is empty.

In this example, notes is a ref array with two notes. newNote is a ref for the form input. addNote pushes a new note object and clears the input. toggleNote flips the done flag for one note. removeNote filters one note out of the array. NoteItem is a small component in the same file, and it shows the note text with Done and Delete buttons. The first render shows Learn ref marked Done and Build a list unmarked. A verified variant with a third note added shows Try v-model in the list. A verified variant with no notes shows No notes yet.

Note

Each note needs a unique id, so :key can tell the notes apart.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

How does the notes app add a note?