← Teacher guide

Data on the page

Students make a Vue app, predict that the page follows the data with no textContent, match the parts of the app, find a plain variable Vue does not watch, add a button and new data, and make a two-value counter.

Apps with Vue · Data on the page · about 50 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min

Before class

Read the unit primer first. A Vue app has two halves. The page is ordinary HTML inside <div id="app">, with {{ name }} wherever a value should appear. The setup function makes the data with ref(...) and returns the names the page may use. createApp({ setup() { … } }).mount("#app") joins them. When a ref changes, Vue redraws the parts of the page that show it: students never write textContent.

vue.js sits beside the page in the playground (it is served from this site). On a real site you would use Vue's file from vuejs.org or a build tool; teacher notes later in the course say more. The console shows one line about a development build: that is the version with friendlier warnings, chosen on purpose.

@click="score++" is a recipe in this lesson (events are lesson 3). The planted bug is a plain let variable: the click changes it but Vue is not watching, so the page never updates and there is no error. That is the most important idea in the course: Vue only keeps up with data it watches.

Running the lesson

  1. Predict: ask students who did the DOM course which line they expect to need.
  2. Run: change the title in setup and run again; nothing else on the page needs to change.
  3. Investigate: for the bug, ask "how does Vue know the score changed?"
  4. Modify: new data is three edits: the ref, the return, the {{ }}. Write them as a checklist on the board.
  5. Make: ask each student which button changes which ref.

The hard part (on purpose)

Students forget one of the three edits (ref, return, {{ }}) and the value does not show. Ask them to point to all three for each piece of data before you look at the code.

When students get stuck

What you will seeA question to askThe fix
The page shows {{ score }} as text.Is the vue.js line there, and does the app mount on #app?Load vue.js before the script, and keep the {{ }} inside the div with id app.
A value is blank and the console warns it was accessed but is not defined.Is its name in the return?Add it: return { title, score, team };
Clicks do nothing and there is no error.Was the data made with ref?const score = ref(0); a plain let is not watched.

Answers to the checks

  1. Predict: You click Add a point three times. What does the page show?
    Score: 3. Each click changes score, and Vue changes every {{ score }} on the page to match.
  2. Which part does what?: Match each part with what it does.
    createApp({ … }).mount("#app") → E. Vue looks after everything inside the #app div; const score = ref(0); → A. data that Vue watches, starting at 0; return { title, score }; → C. the names the page can use; {{ score }} → D. shows the data, and keeps it up to date; @click="score++" → B. changes the data when the button is clicked
    Data in setup, the page in #app. Change the data, and Vue changes the page.
  3. Without Vue: In the DOM course, which line would this page also need?
    scoreEl.textContent = score;
    Without Vue you change the variable, then put it on the page yourself. Vue does the second job.
  4. Spot the bug: The page shows Score: 0, and clicking does nothing. There is no red error. Click the line that is wrong.
    line 11
    let score = 0 is a plain variable, so Vue does not watch it. The click changes it, but Vue never knows. Make it with ref(0).
  5. Where this leads: Your page needs to show a list of players, one line each. What would you rather write?
    One line that says: one <li> for each player
    Next lesson: v-for makes a tag for each item in a list, and keeps it in step as the list changes.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Take a point

<div id="app">
  <h1>{{ title }}</h1>
  <p>Score: {{ score }}</p>
  <button @click="score++">Add a point</button>
  <button @click="score--">Take a point</button>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  createApp({
    setup() {
      const title = ref("Quiz night");
      const score = ref(0);
      return { title, score };
    }
  }).mount("#app");
</script>

Modify › A team name

<div id="app">
  <h1>{{ title }}</h1>
  <p>Team: {{ team }}</p>
  <p>Score: {{ score }}</p>
  <button @click="score++">Add a point</button>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  createApp({
    setup() {
      const title = ref("Quiz night");
      const team = ref("Owls");
      const score = ref(0);
      return { title, team, score };
    }
  }).mount("#app");
</script>

a counter of your own

<div id="app">
  <h1>Space game</h1>
  <p>Lives: {{ lives }}</p>
  <p>Level: {{ level }}</p>
  <button @click="lives--">Lose a life</button>
  <button @click="level++">Next level</button>
</div>

<script src="vue.js"></script>
<script>
  const { createApp, ref } = Vue;
  createApp({
    setup() {
      const lives = ref(3);
      const level = ref(1);
      return { lives, level };
    }
  }).mount("#app");
</script>

Stretch and support

Stretch: Show the score twice on the page (in the title and a paragraph) and watch both follow. Add a button that doubles the score.

Support: Give the finished run page and ask students only to add the Take a point button.

Exit ticket

What two things must you do so a value in setup appears on the page and stays up to date?

Make it with ref and return it from setup, then show it with {{ name }} inside the app.

What each step is for

Where it leads

Next lesson: lists with v-for and choices with v-if. Class idea: list three apps and name the data each one keeps.

Sources