← Teacher guides

Teacher guide: Apps with Vue

Build pages that keep up with their data by themselves: data shown with {{ }}, lists with v-for, choices with v-if, events and inputs, computed values and components, ending in a to-do app. An optional last lesson builds the same app in Vue, Preact and Svelte.

Open the student course · How the ideas build

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Strategies for getting unstuck: Say what it should do, Test a small piece, Read the error, Trace it. A student who is stuck can try one before asking for a hint. The optional course How you learn best introduces them, and a few lesson decks have a short teach, show or nudge slide for one. (Each lesson page links to the cards from its first hint.)

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Data on the page

Show data, make a tag for each item, show a tag only when it is needed, and change the data from clicks and typing.

Vue in 5 minutes (read before teaching this unit)

The idea in plain words

Vue is a JavaScript framework: code someone else wrote that does the repetitive work of keeping a page up to date. You keep the data (made with ref, so Vue watches it) and write the page once, with the data in it: {{ score }} shows a value, v-for makes a tag for each item in a list, v-if shows a tag only when something is true, v-model joins a box to data, and @click runs a function. When the data changes, Vue changes the page. Nobody writes textContent or createElement any more.

Why programmers use it

  • Less code, fewer slips: in the DOM course every change needed two jobs (change the variable, then the page). Vue does the second.
  • The page always matches the data: nothing can be forgotten.
  • Bigger apps stay manageable: components (unit 2) split a page into parts.

An everyday picture, and where it breaks

A scoreboard operator who watches the game and updates the board without being asked. You only change the score. It breaks because the operator only watches what you point out to them: data made with ref. A plain variable changes in silence, and the board never moves.

Try it yourself (5 minutes)

  1. In the Vue playground, change ref(0) to ref(10) and run it.
  2. Show the count twice on the page and click: both change.
  3. Change const count = ref(0) to let count = 0 and click. Nothing happens, and there is no error.

Questions students ask

  • When do I write .value? In the script, always (score.value++). On the page, never ({{ score }}).
  • Why does my page show {{ score }} as text? Vue did not start: check the vue.js line, and that the tag is inside #app.
  • What is the "development build" message? The version of Vue with extra warnings for learners. On a real site you would use the production one.

How it connects

It replaces the DOM course's two jobs with one. Bootstrap did the same for CSS: borrowed code for repetitive work. React and Svelte share the same ideas (the optional last lesson).

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Open in Print · Markdown

Building apps

Values worked out from data with computed, and components you make once and use many times.

Computed values and components in 5 minutes (read before teaching this unit)

The idea in plain words

Two tools for building real apps. A computed value is worked out from other data, computed(() => adults.value * 12), and Vue works it out again whenever that data changes: a basket total, a count of unread messages. A component is a tag of your own, app.component("player-card", { props: [...], template: ... }), written once and used many times; props are the data each copy is given, passed with a colon (:wins="p.wins").

Why programmers use it

  • Computed values can't go stale: they are never set by hand, only worked out.
  • Components remove copy and paste: change the template once and every card changes.
  • Props make components reusable: the same card shows any player.

An everyday picture, and where it breaks

A spreadsheet: a total cell is a formula, so it updates when the numbers change (computed), and a template letter with blanks is filled in for each person (a component with props). It breaks because a spreadsheet works out every formula; Vue works out only values made with computed. A plain calculation in setup is like typing the total in by hand: right once, then wrong.

Try it yourself (5 minutes)

  1. In the tickets page, change the adult price to 15 and watch the total.
  2. Change computed(…) back to a plain calculation and click: the total stops.
  3. In the ladder page, take the colon off :wins and see what the cards show.

Questions students ask

  • Ref or computed? If you set it, a ref. If you work it out from other data, a computed.
  • Why does my card show p.wins? The colon is missing: :wins="p.wins".
  • Can a card change its own props? No: data flows down. Change it in the app's data instead.

How it connects

The final challenge uses both: a computed count of tasks left and a component for each task.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Open in Print · Markdown

Final challenge: a to-do app

Build a to-do app in milestones. One optional lesson builds the same app in three frameworks.

Running the final challenge (read before teaching this unit)

The idea in plain words

Students build a to-do app from everything in the course, in three milestones with fading support: most of the code, then an outline in comments, then only the goals. Each milestone has its own starting point (Reset to this task's start), so a student who falls behind can still finish. Then they add a feature of their own. The optional lesson builds the same kind of app in Vue, Preact and Svelte.

Why programmers use it

The to-do list is the classic first app for every framework, because it needs data, events, a worked-out value and a component, and nothing else.

An everyday picture, and where it breaks

Building flat-pack furniture with the instructions thinning out: step-by-step for the first drawer, a diagram for the second, just a photo for the third. It breaks if a student skips the first two: the milestones are the instructions.

Try it yourself (5 minutes)

Build milestone 1 yourself before the lesson, then open the finished app and tick a task: say which Vue piece makes each thing happen.

Questions students ask

  • Why use a for loop for the count? It uses what you know. filter is a shorter way, for later.
  • Will it remember my tasks? Not yet: refresh the page and they are gone. Saving them (localStorage) is a stretch.
  • Is this how real apps are made? Yes, with a build tool and one file per component; the ideas are the same.

How it connects

The optional lesson compares Vue with Preact (React's small cousin) and Svelte, so students see which ideas belong to all frameworks.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Open in Print · Markdown

Open projects

Self-paced briefs students can pick up (after the lesson named on each, where one is named). Students tick their own milestones and claim a badge; the page lights the skills their code uses. Badges stay in the student's browser: ask them to press Show your teacher. Open the projects page

Class poll (after vue-events; data Vue watches (ref), showing data ({{ score }}), Vue events (@click), a tag only when (v-if, v-else))

Make a class poll: ask a question with three answers. Each click adds a vote, the totals update by themselves, and a message changes once someone has voted.

Milestones:

  • Every answer has a button, and each click counts.
    1. Each answer needs its own number kept in data.
    2. A button can use @click to add one to its number.
    Students can earn this piece (reveal on): <button @click="votes.pizza++">Pizza ({{ votes.pizza }})</button>
  • The totals on the page update by themselves.
    1. Put the number in double curly brackets, and the page keeps up with the data.
  • A message shows before anyone votes, and a different one after.
    1. v-if shows a tag only when its test is true. v-else goes straight after it.
    2. The test can add up all the answers and check if the total is more than zero.
    Students can earn this piece (reveal on): <p v-if="votes.pizza + votes.tacos + votes.sushi > 0">Thanks for voting! {{ votes.pizza + votes.tacos + votes.sushi }} votes so far.</p>
    <p v-else>No votes yet. Be the first!</p>

Data on the page and Events and inputs cover all of it. The point to draw out: nothing in the code updates the page; changing the data is enough.

One possible solution
<div id="app">
  <h1>{{ question }}</h1>
  <p>
    <button @click="votes.pizza++">Pizza ({{ votes.pizza }})</button>
    <button @click="votes.tacos++">Tacos ({{ votes.tacos }})</button>
    <button @click="votes.sushi++">Sushi ({{ votes.sushi }})</button>
  </p>
  <p v-if="votes.pizza + votes.tacos + votes.sushi > 0">Thanks for voting! {{ votes.pizza + votes.tacos + votes.sushi }} votes so far.</p>
  <p v-else>No votes yet. Be the first!</p>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  createApp({
    setup() {
      const question = ref("What should we have for the class lunch?");
      const votes = ref({ pizza: 0, tacos: 0, sushi: 0 });
      return { question, votes };
    }
  }).mount("#app");
</script>
Reading log (after vue-computed; a tag for each item (v-for), a box joined to data (v-model), a value worked out from data (computed))

Keep a reading log: a list of books with a rating, a box that filters the list as you type, and a line that says how many you've read.

Milestones:

  • My books show from a list.
    1. Keep the books in a list in data, one object for each book.
    2. v-for makes one tag for each item in the list.
    Students can earn this piece (reveal on): <li v-for="b in shown">{{ b.title }}: {{ b.stars }} stars</li>
  • Adding a book puts it on the list.
    1. A text box with v-model keeps what is typed in a data value.
    2. A function can push a new object onto the list, using that value.
    Students can earn this piece (reveal on): books.value.push({ title: newTitle.value, stars: 3 });
  • The filter and the count are both worked out from the list, so they keep up on their own.
    1. A computed value is worked out from other data, and it updates when that data changes.
    2. Do not change the list itself to filter it. Make a new computed value that is the view you want.
    Students can earn this piece (reveal on): const count = computed(() => books.value.length);

The filter is the computed idea from Values worked out from data: the list itself never changes when the student types, only the computed view of it. Students who filter by changing the list itself lose books; that's a good moment to compare.

One possible solution
<div id="app">
  <h1>Reading log</h1>
  <p><input v-model="search" placeholder="Filter by title"></p>
  <ul>
    <li v-for="b in shown">{{ b.title }}: {{ b.stars }} stars</li>
  </ul>
  <p>{{ count }} books read.</p>
  <p><input v-model="newTitle" placeholder="New book"> <button @click="add">Add</button></p>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref, computed } = Vue;
  createApp({
    setup() {
      const books = ref([
        { title: "Wonder", stars: 5 },
        { title: "Holes", stars: 4 },
        { title: "Matilda", stars: 5 }
      ]);
      const search = ref("");
      const newTitle = ref("");
      const shown = computed(() => books.value.filter((b) => b.title.toLowerCase().includes(search.value.toLowerCase())));
      const count = computed(() => books.value.length);
      function add() {
        if (newTitle.value === "") return;
        books.value.push({ title: newTitle.value, stars: 3 });
        newTitle.value = "";
      }
      return { search, newTitle, shown, count, add };
    }
  }).mount("#app");
</script>
Card gallery (after vue-components; a component of your own (app.component), props, and :name="value" to pass data in, a tag for each item (v-for))

Make one card component and use it for every item in a gallery: pets to adopt, players in a team, places to visit.

Milestones:

  • The card is written once.
    1. A component is a name, and a template that says what the card looks like.
    Students can earn this piece (reveal on): app.component("pet-card", {
    props: ["name", "kind", "age"],
  • Each card gets its own data through props.
    1. Props are the blanks a card has. The tag that uses the card fills each one in.
    2. Pass a prop with a colon in front of its name, such as :name.
    Students can earn this piece (reveal on): <pet-card v-for="p in pets" :name="p.name" :kind="p.kind" :age="p.age"></pet-card>
  • Adding an item to the list adds a card, with no new HTML.
    1. If the cards come from a v-for over a list, a new item in the list is a new card.

The same shape as the chess ladder in Components and props. A good check of understanding: ask the student where they'd change the look of every card at once, and where they'd add one more item.

One possible solution
<div id="app">
  <h1>Pets to adopt</h1>
  <pet-card v-for="p in pets" :name="p.name" :kind="p.kind" :age="p.age"></pet-card>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  const app = createApp({
    setup() {
      const pets = ref([
        { name: "Biscuit", kind: "dog", age: 3 },
        { name: "Mango", kind: "cat", age: 1 },
        { name: "Pip", kind: "rabbit", age: 2 }
      ]);
      return { pets };
    }
  });
  app.component("pet-card", {
    props: ["name", "kind", "age"],
    template: `
      <div style="border: 2px solid teal; border-radius: 8px; padding: 8px 12px; margin: 8px 0;">
        <h2>{{ name }}</h2>
        <p>A {{ kind }}, {{ age }} years old.</p>
      </div>`
  });
  app.mount("#app");
</script>