Summary
In this lesson, you learned that:
- Vue renders the template again only when reactive data changes. A plain variable can change, but the page won't show it.
ref(), imported withimport { ref } from "vue";, makes reactive state:const count = ref(0);. Refs are alwaysconst: the ref stays the same, its value changes.- In the script, the value is
count.value. In the template, Vue unwraps refs, so it's justcount.count++in the script ends withTypeError: Assignment to constant variable., and{{ count.value }}in the template shows nothing. @clickis short forv-on:click. Its value is a function's name, a call with arguments such asaddToOrder('Espresso'), which Vue runs on the click, not at once, or a short statement such ascount++. Anything longer goes into a function in the script.- A handler named without parentheses gets the event object. When you pass arguments of your own, the event object is
$event. @works with any event:@inputandevent.target.valuefor typing (with:valueto put a ref into the field),@keydown.enterfor Enter, and@mouseoverand@mouseleavefor the mouse.- A ref can hold an array or an object.
push(),pop(),splice()and changes of properties such asorder.value.note = "No sugar"all update the page, and so does a new array for.value, for example fromfilter().
In the next lesson, Conditions and Lists, you'll show parts of the page only when a condition holds, and draw a list from an array, with an element for each item.
But first, a little practice: a waiting list for the café's tables. App.vue has only the heading, and you'll write the rest yourself.
Instructions
- 1
Under the heading, add a text field for the name of a guest. Keep what's typed in a ref
name(with@input), and bind the field to it with:value. When Enter is pressed in the field, add the name to the end of a refwaiting, which holds an array, and empty the field. Under the field, show the list in a paragraph with the classwaiting:Waiting: Anna, Ben. - 2
Add a button Seat next under the list. It takes the first name off the waiting list (with
splice(0, 1)) and shows it in a paragraph with the classseated:Seated: Anna. When nobody is waiting, the button does nothing.