TExcelTAD
Home › Vue › Vue Mini Project

Vue Mini Project

In this project you'll build a shopping list app step by step, using most of what you've learned in this course: reactive state, v-for, v-model, events, computed properties, components with props and emits, watchers and transitions. Each step is a complete, runnable playground that grows from the previous one — run it, read the changes, then try the challenge at the end.

Step New feature Concepts
1 Show a list ref, v-for, :key
2 Add and remove items v-model, @submit.prevent, methods
3 Mark bought, filters, counters computed, :class, v-if
4 Split into components props, emits, slots
5 Polish watch, <transition-group>, template refs

Step 1 — Show the list

Start with data and a template. Every item has a unique id for the key.

html · try it yourself
<div id="app">
  <h2>🛒 Shopping list</h2>
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.qty }} × {{ item.name }}</li>
  </ul>
</div>

<script type="module">
  import { createApp, ref } from 'vue';

  createApp({
    setup() {
      const items = ref([
        { id: 1, name: 'Rice', qty: 1, bought: false },
        { id: 2, name: 'Eggs', qty: 12, bought: true },
        { id: 3, name: 'Bananas', qty: 6, bought: false }
      ]);
      return { items };
    }
  }).mount('#app');
</script>

Step 2 — Add and remove items

A form with two inputs bound with v-model. @submit.prevent handles both the button and the Enter key. New ids come from a counter, so they stay unique.

html · try it yourself
<div id="app">
  <h2>🛒 Shopping list</h2>
  <form @submit.prevent="addItem">
    <input v-model.trim="newName" placeholder="Item name">
    <input v-model.number="newQty" type="number" min="1" style="width: 50px">
    <button :disabled="!newName">Add</button>
  </form>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.qty }} × {{ item.name }}
      <button @click="removeItem(item.id)" title="Remove">✕</button>
    </li>
  </ul>
</div>

<script type="module">
  import { createApp, ref } from 'vue';

  createApp({
    setup() {
      let nextId = 4;
      const items = ref([
        { id: 1, name: 'Rice', qty: 1, bought: false },
        { id: 2, name: 'Eggs', qty: 12, bought: true },
        { id: 3, name: 'Bananas', qty: 6, bought: false }
      ]);
      const newName = ref('');
      const newQty = ref(1);

      function addItem() {
        if (!newName.value) return;
        items.value.push({ id: nextId++, name: newName.value, qty: newQty.value || 1, bought: false });
        newName.value = '';
        newQty.value = 1;
      }
      function removeItem(id) {
        items.value = items.value.filter(i => i.id !== id);
      }
      return { items, newName, newQty, addItem, removeItem };
    }
  }).mount('#app');
</script>

Step 3 — Bought items, filters and counters

Now a checkbox marks items as bought, a computed visibleItems applies the chosen filter, and other computed values show the progress. The original items array is never filtered in place.

html · try it yourself
<style>
  .bought { text-decoration: line-through; color: #9ca3af; }
  .filters button.active { background: #42b883; color: white; border-color: #42b883; }
</style>

<div id="app">
  <h2>🛒 Shopping list <small>({{ boughtCount }}/{{ items.length }} bought)</small></h2>
  <form @submit.prevent="addItem">
    <input v-model.trim="newName" placeholder="Item name"> <button :disabled="!newName">Add</button>
  </form>

  <p class="filters">
    <button v-for="f in ['all', 'open', 'bought']" :key="f" :class="{ active: filter === f }" @click="filter = f">{{ f }}</button>
  </p>

  <ul v-if="visibleItems.length">
    <li v-for="item in visibleItems" :key="item.id">
      <label :class="{ bought: item.bought }">
        <input type="checkbox" v-model="item.bought"> {{ item.qty }} × {{ item.name }}
      </label>
      <button @click="removeItem(item.id)">✕</button>
    </li>
  </ul>
  <p v-else>Nothing here 🎉</p>

  <button v-if="boughtCount" @click="clearBought">Remove bought items</button>
</div>

<script type="module">
  import { createApp, ref, computed } from 'vue';

  createApp({
    setup() {
      let nextId = 4;
      const items = ref([
        { id: 1, name: 'Rice', qty: 1, bought: false },
        { id: 2, name: 'Eggs', qty: 12, bought: true },
        { id: 3, name: 'Bananas', qty: 6, bought: false }
      ]);
      const newName = ref('');
      const filter = ref('all');

      const visibleItems = computed(() => {
        if (filter.value === 'open') return items.value.filter(i => !i.bought);
        if (filter.value === 'bought') return items.value.filter(i => i.bought);
        return items.value;
      });
      const boughtCount = computed(() => items.value.filter(i => i.bought).length);

      function addItem() {
        items.value.push({ id: nextId++, name: newName.value, qty: 1, bought: false });
        newName.value = '';
      }
      const removeItem = (id) => { items.value = items.value.filter(i => i.id !== id); };
      const clearBought = () => { items.value = items.value.filter(i => !i.bought); };

      return { items, newName, filter, visibleItems, boughtCount, addItem, removeItem, clearBought };
    }
  }).mount('#app');
</script>

Step 4 — Components

The root component is getting long. Split it: list-item shows one row and emits toggle and remove; the parent still owns the data. A <slot> lets the parent decide what text the empty state shows.

html · try it yourself
<style>
  .row { display: flex; gap: 8px; align-items: center; padding: 4px 0; }
  .bought { text-decoration: line-through; color: #9ca3af; }
  .empty { color: #6b7280; font-style: italic; }
</style>

<div id="app">
  <h2>🛒 Shopping list</h2>
  <item-form @add="addItem"></item-form>
  <item-list :items="items" @toggle="toggle" @remove="removeItem">
    Your list is empty — add something above!
  </item-list>
</div>

<script type="module">
  import { createApp, ref } from 'vue';

  const app = createApp({
    setup() {
      let nextId = 3;
      const items = ref([
        { id: 1, name: 'Rice', qty: 1, bought: false },
        { id: 2, name: 'Eggs', qty: 12, bought: true }
      ]);
      const addItem = ({ name, qty }) => items.value.push({ id: nextId++, name, qty, bought: false });
      const toggle = (id) => { const i = items.value.find(i => i.id === id); i.bought = !i.bought; };
      const removeItem = (id) => { items.value = items.value.filter(i => i.id !== id); };
      return { items, addItem, toggle, removeItem };
    }
  });

  app.component('item-form', {
    emits: ['add'],
    setup(props, { emit }) {
      const name = ref('');
      const qty = ref(1);
      function submit() {
        if (!name.value.trim()) return;
        emit('add', { name: name.value.trim(), qty: qty.value || 1 });
        name.value = '';
        qty.value = 1;
      }
      return { name, qty, submit };
    },
    template: `
      <form @submit.prevent="submit">
        <input v-model="name" placeholder="Item name">
        <input v-model.number="qty" type="number" min="1" style="width:50px">
        <button>Add</button>
      </form>`
  });

  app.component('list-item', {
    props: { item: { type: Object, required: true } },
    emits: ['toggle', 'remove'],
    template: `
      <li class="row">
        <input type="checkbox" :checked="item.bought" @change="$emit('toggle', item.id)">
        <span :class="{ bought: item.bought }">{{ item.qty }} × {{ item.name }}</span>
        <button @click="$emit('remove', item.id)">✕</button>
      </li>`
  });

  app.component('item-list', {
    props: ['items'],
    emits: ['toggle', 'remove'],
    template: `
      <ul v-if="items.length">
        <list-item v-for="i in items" :key="i.id" :item="i"
                   @toggle="$emit('toggle', $event)" @remove="$emit('remove', $event)" />
      </ul>
      <p v-else class="empty"><slot>Empty.</slot></p>`
  });

  app.mount('#app');
</script>

Notice how item-list re-emits the events of list-item: component events don't bubble, so each level passes them up explicitly. (With more nesting, a store would be simpler.)

Step 5 — Polish

The final version adds:

  • a shared store made with reactive() (Pinia-style: state + actions),
  • a watcher that keeps a change history (stand-in for saving to a server),
  • <transition-group> animations when items come and go,
  • a template ref to focus the name input after adding.
html · try it yourself
<style>
  body { font-family: system-ui, sans-serif; }
  .app { max-width: 360px; }
  .row { display: flex; gap: 8px; align-items: center; padding: 6px 8px; margin: 4px 0; background: #f0fdf4; border-radius: 6px; }
  .row span { flex: 1; }
  .bought span { text-decoration: line-through; color: #9ca3af; }
  ul { list-style: none; padding: 0; position: relative; }
  .fade-move, .fade-enter-active, .fade-leave-active { transition: all .35s ease; }
  .fade-enter-from, .fade-leave-to { opacity: 0; transform: translateX(24px); }
  .fade-leave-active { position: absolute; width: 100%; }
  .bar { height: 8px; background: #e5e7eb; border-radius: 4px; overflow: hidden; }
  .bar div { height: 100%; background: #42b883; transition: width .3s; }
</style>

<div id="app" class="app">
  <h2>🛒 Shopping list</h2>
  <form @submit.prevent="add">
    <input ref="nameInput" v-model.trim="name" placeholder="Item name">
    <button :disabled="!name">Add</button>
  </form>

  <p>{{ store.boughtCount }} of {{ store.items.length }} bought</p>
  <div class="bar"><div :style="{ width: progress + '%' }"></div></div>

  <transition-group name="fade" tag="ul">
    <li v-for="item in store.sortedItems" :key="item.id" class="row" :class="{ bought: item.bought }">
      <input type="checkbox" :checked="item.bought" @change="store.toggle(item.id)">
      <span>{{ item.name }}</span>
      <button @click="store.remove(item.id)">✕</button>
    </li>
  </transition-group>

  <p><small>Changes saved: {{ saves }}</small></p>
</div>

<script type="module">
  import { createApp, ref, reactive, computed, watch } from 'vue';

  // ---- store (stores/shopping.js in a real project) ----
  let nextId = 4;
  const store = reactive({
    items: [
      { id: 1, name: 'Rice', bought: false },
      { id: 2, name: 'Eggs', bought: true },
      { id: 3, name: 'Bananas', bought: false }
    ],
    // getters (reactive() unwraps these computed refs)
    boughtCount: computed(() => store.items.filter(i => i.bought).length),
    sortedItems: computed(() => [...store.items].sort((a, b) => a.bought - b.bought)),  // open items first
    // actions
    add(name) { store.items.push({ id: nextId++, name, bought: false }); },
    toggle(id) { const i = store.items.find(i => i.id === id); i.bought = !i.bought; },
    remove(id) { store.items = store.items.filter(i => i.id !== id); }
  });

  createApp({
    setup() {
      const name = ref('');
      const nameInput = ref(null);
      const saves = ref(0);
      const progress = computed(() =>
        store.items.length ? Math.round(store.boughtCount / store.items.length * 100) : 0);

      function add() {
        if (!name.value) return;
        store.add(name.value);
        name.value = '';
        nameInput.value.focus();          // template ref
      }

      // "persist" every change (deep: also item.bought changes)
      watch(() => store.items, (items) => {
        saves.value++;
        console.log('saved:', items.map(i => (i.bought ? '✓ ' : '· ') + i.name).join(', '));
      }, { deep: true });

      return { store, name, nameInput, saves, progress, add };
    }
  }).mount('#app');
</script>

Tick items and watch them slide to the bottom of the list, while the progress bar and counter update and every change is "saved".

Challenges

Try extending the final version on your own:

  1. Edit in place — double-click an item name to turn it into an input (v-if + a template ref to focus it), and save on Enter.
  2. Quantities — add a quantity stepper component that works with v-model (modelValue + update:modelValue).
  3. Categories — group items by category ("Fruit", "Dairy"…) with a computed property that returns an object of arrays, and render each group with a heading.
  4. Undo — keep the last removed item in the store and show an "Undo" button inside a <teleport to="body"> toast for a few seconds.

Tip: When a feature feels hard, go back to its lesson: every challenge uses only techniques from this course.

Note: To turn this into a real project, run npm create vue@latest, put each component into its own .vue file with <script setup>, move the store into a Pinia store, and replace the history watcher with fetch calls to your API.

Test yourself

In step 3, why does visibleItems use filter() in a computed property instead of changing items?

  1. filter() is faster
  2. So the original list stays intact and the view updates automatically when the filter changes
  3. Computed properties can't read refs
  4. To avoid using v-for

In step 4, how does a click in list-item reach the root component?

  1. The event bubbles automatically
  2. item-list listens and re-emits the event
  3. Through a prop
  4. Via v-model

Why does the watcher in step 5 use { deep: true }?

  1. To run immediately
  2. So ticking an item's bought property (a nested change) also triggers it
  3. To watch the DOM
  4. Deep is required for arrays of strings