Learn What Is Vue?

Vue is a JavaScript framework (a set of ready-made code) made by Evan You (2014) for building interfaces out of components. A component is a piece of the…

Picture the digital price board in front of Mrs. Sri's stall. Mrs. Sri only changes the iced tea price in the cashier's book, and the number on the board changes by itself. Nobody climbs a ladder to repaint it.

Vue is a JavaScript framework for building web interfaces. A framework is a set of ready-made code that decides how you organise an app. Vue works like Mrs. Sri's price board: you write the data and the shape of the page, and Vue keeps the page matching the data. The cashier's book is the data, the board is the page, and the one who changes the number on the board is Vue. Throughout this Vue track we build one example: the menu and cart of Mrs. Sri's stall.

Without Vue: you manage the DOM

The DOM is the set of page elements that JavaScript can read and change. With plain JavaScript, changing the data and changing the page are two separate jobs. This code shows a price, and has a function to raise it:

let price = 5
const span = document.querySelector("#price")
span.textContent = "$" + price

function raisePrice() {
  price = price + 1
  // forget this line and the screen stays $5
  span.textContent = "$" + price
}

With Vue: write the data and the template

In Vue, a piece of the interface together with its data is called a component. Here is the price board component, the way you write it in the BelajarWeb.dev exercise editor:

const price = ref(5)

function raisePrice() {
  price.value = price.value + 1
}

---template---
<h2>Mrs. Sri's Stall</h2>
<p>Iced Tea: <span id="price">${{ price }}</span></p>
<button @click="raisePrice">Raise price</button>

The data changes, the view follows

This table shows the data and the page each time the button is clicked:

What happens price.value What the <span> shows
The page first shows 5 $5
Click Raise price 6 $6
Click again 7 $7

You don't write any extra code for the <span>. Vue does the work in three steps:

  1. When the template is shown the first time, Vue notes that this component reads price.
  2. As soon as price.value changes, Vue builds the view from the template again and compares it with the previous one.
  3. Vue replaces only the part that differs: the text in the <span>.

This ability is called reactivity: the data changes, and the view changes with it. If price is used in three places, all three places are updated at once.

createApp and mount in a real project

In the exercise editor, BelajarWeb.dev mounts your component on the page. In a real project (made with the command npm create vue@latest in a terminal, the window where you type commands), you mount it yourself in the file main.js:

// main.js
import { createApp } from "vue"
import App from "./App.vue"

createApp(App).mount("#app")

The index.html file has one empty element as the place to mount it:

<!-- index.html -->
<div id="app"></div>

Who made Vue, and why people use it

⚠️ Blank page, or a number that won't change? Usually one of these

TL;DR: Vue is a JavaScript framework (a set of ready-made code) made by Evan You (2014) for building interfaces out of components. A component is a piece of the interface together with its data. It has a script (data and functions) and a template (HTML plus Vue syntax). const price = ref(5) makes reactive data, which is data that Vue watches. In the script you read it through price.value. In the template, just {{ price }}. When that data changes, Vue updates the parts of the page that read it, and you don't change the page elements (the DOM) yourself. That is called reactivity. In a real project, the app is created and mounted on the page with createApp(App).mount("#app"), and the selector needs the #. In the exercise editor, write the script, the ---template--- line, then the template. An import line is not needed there.

⚡ Reactive data with ref

Write:

const price = ref(5)

function raisePrice() {
  price.value = price.value + 1
}

---template---
<h2>Mrs. Sri's Stall</h2>
<p>Iced Tea: <span id="price">${{ price }}</span></p>
<button @click="raisePrice">Raise price</button>

Meaning: Vue is a JavaScript framework (a set of ready-made code) for building web interfaces. This code is one Vue component: a piece of the interface together with its data. Above the ---template--- line (the exercise editor's divider) is the script (data and functions). Below the line is the template (its HTML). ref(5) makes reactive data, which is data that Vue watches. In the script you read and change it through .value. In the template, {{ price }} shows the value without .value, and @click runs raisePrice when the button is clicked.

⚡ The data changes, the view follows

Guess first: in that component, not one line changes the <span>. When Raise price is clicked, does the number change?

It does, from $5 to $6. The click runs raisePrice, which adds 1 to price.value. Vue noted that the template reads price. So as soon as price.value changes, Vue itself replaces the text in the <span>.

Meaning: This ability is called reactivity: the data changes, and the view changes with it. If price is used in three places in the template, all three places are updated at once.

⚡ Mount it on the page with createApp

Write:

import { createApp } from "vue"
import App from "./App.vue"

createApp(App).mount("#app")
Guess first: mount("#app") attaches the Vue application to the element that matches the CSS selector #app. What happens if you write mount("app"), without the #?

The page stays empty. Without #, the selector looks for an <app> tag, not the element with the id app. There is no <app> tag on the page.

Meaning: In a real project, the import lines get createApp and the App component. createApp(App) makes a Vue application from the App component, and .mount("#app") attaches that application to <div id="app"> in the HTML file. In the exercise editor, BelajarWeb.dev mounts it for you.

What you will learn