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
}
- The second line gets the
<span>element with the idpricefrom the page. price = price + 1only changes a variable. The text on the page has not changed yet.span.textContent = ...is the line that actually replaces the text on the page. You have to write it yourself.- If the price shows in three places (menu, cart, receipt), you need three update lines. If you forget one, the page shows the wrong 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 part above the
---template---line is the script: the component's data and functions. ref(5)makes reactive data, which is data that Vue watches. In the script, you read and change it through.value.- The part below the line is the template: HTML that may use Vue's own syntax (way of writing).
{{ price }}(two pairs of curly braces) puts the value ofpriceat that place on the page. You don't need.valuein the template, because Vue gets the value itself.@click="raisePrice"calls the functionraisePricewhen the button is clicked. Events (things that happen, such as a click) get their own lesson later.- In the exercise editor,
refis already available, so the lineimport { ref } from "vue"is not needed.
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:
- When the template is shown the first time, Vue notes that this component reads
price. - As soon as
price.valuechanges, Vue builds the view from the template again and compares it with the previous one. - 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>
- The two
importlines get the functioncreateAppfrom Vue and the componentAppfrom the fileApp.vue. createApp(App)makes a Vue application withAppas the outermost component, called the root component..mount("#app")attaches that application to the element that matches the CSS selector#app, which is<div id="app">. The content of that element is replaced by Vue's view.- Without the
#,"app"looks for an<app>tag. There is no such tag, so the page stays empty. In development mode (the version of Vue for while you are building), the console says[Vue warn]: Failed to mount app: mount target selector "app" returned null.The exercise editor and the Playground load the production build (the version of Vue for users), which writes no[Vue warn]at all. So warnings like this only show in your own project, when you run it withnpm run dev. - A component can also be an object, such as
createApp({ template: "<h1>Hello</h1>" }).mount("#app"). This only works when the version of Vue that is loaded can compile templates in the browser, like the version in the exercise editor.
Who made Vue, and why people use it
- Vue was made by Evan You and released in 2014. Today a core team and an open source community develop it (the code is public, and anyone may help improve it). This track uses Vue 3.5.
- Vue is called progressive because you can use a little of it first and add more later. You can add Vue to one part of a page only. You can also build a whole application with it, together with Vue Router (for moving between pages) and Pinia (for data that many components use).
- There are two styles of writing components. The Composition API uses
<script setup>andref(). It is the modern style used in this track. The Options API usesdata()andmethods. It is the older style you often see in Vue 2 tutorials, but Vue 3 still fully supports it. Vue's official site suggests the Options API if you use no build tool, and the Composition API for full applications. That is why this track uses the Composition API.
⚠️ Blank page, or a number that won't change? Usually one of these
mount("app")without#. A selector without#looks for an<app>tag, not an id. The page stays empty. Writemount("#app").- Forgetting
import { ref } from "vue"in a real project. The exercise editor providesrefwithout an import, but in a real.vuefile the line is required. Without that line, the code stops withReferenceError: ref is not defined. {{ price.value }}in the template. In the template, Vue has already taken the value out of the reactive data, sopriceis the number 5. A number has no.value, so the result isundefinedand nothing shows. In the template, write just{{ price }}.- Keeping data in a plain variable. The value of
let price = 5shows once, but its changes never reach the screen, because Vue does not watch plain variables. Data that should change the view is made withref().
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.