Summary
In this lesson, you learned that:
- Vue makes the page from a template and data. When the data changes, Vue updates the page by itself, so you don't need
querySelector(),textContentorcreateElement(). - A Vue app is made of a few files:
index.htmlwith an empty<div id="app">;main.js, which mounts the componentAppinto it withcreateApp(App).mount("#app")and is the same in every exercise; the component itself,App.vue; andstyle.csswith the app's styles. - A
.vuefile has up to three parts:<script setup>with JavaScript,<template>with what the component shows, and<style scoped>with CSS that applies only to this component. {{ cafe }}in the template shows the value of the variablecafefrom<script setup>. Single braces are just text.- A value that changes while the app runs is a ref:
const count = ref(0);, withimport { ref } from "vue";at the top of the script. In the script, its value iscount.value; in the template, justcount.@click="addEspresso"runs a function on every click. - A mistake in a file shows in red under the preview, with its line and column, and the app doesn't run. Errors while the app runs and Vue's warnings, which start with
[Vue warn], show in the console.
In the next lesson, Template Syntax, you'll put more than names into {{ }}: calculations, and choices between two texts. And you'll set attributes, classes and styles from your data.
But first, a little practice: the café's cake of the day, with a button for ordering a slice.
Instructions
- 1
Under the heading, add a paragraph that shows the cake and its price from the variables
cakeandprice:Cheesecake, €5. - 2
Count the slices ordered: create a ref
slicesthat starts at 0 (with theimportline it needs), and show it under the price in a paragraph:Slices ordered: 0. - 3
Add a button
Order a slicethat increasesslicesby 1 on every click. Write a function for it, asaddEspressowas.