Summary
In this lesson, you learned that:
v-if="condition"puts an element on the page only while the condition is true.v-else-ifandv-elsecome right after it, and Vue shows only the first element whose condition is true. Eachv-ifstarts a new chain.v-showkeeps the element on the page and only hides it, withdisplay: none. It's for things that are shown and hidden often; otherwise, usev-if.v-for="item in menu"makes an element for each item of an array. It needs:key="item.id", a key that stays with its item, not the item's position, which changes when items are removed.(item, index) in menugives you the position too, from 0, andn in 5counts from 1 to 5.<template>groups elements forv-iforv-for, and isn't on the page itself.v-ifandv-fornever go on the same element: put thev-foron a<template>around it.- An empty list deserves a message:
v-if="order.length === 0"on the message andv-elseon the list. v-forcan go over what a function returns, such asitemsIn('tea'), which filters or sorts. Sort a new array, never your state itself.
In the next lesson, Checkpoint: Packing List, you'll put the last four lessons together in a packing list for a weekend in the mountains.
But first, a little practice: the café's tables, which are free or taken.
Instructions
- 1
In the list, show an
lifor each table, withTableand its number, and then a chair🪑for each of its seats:Table 1 🪑🪑. - 2
After the chairs, show
FreeorTaken(withv-ifandv-else), and a button Change, which switches the table between free and taken. - 3
Under the list, show how many tables are free,
Free tables: 2, orAll tables are taken.when none is. Get the free tables with a functionfreeTables(), which returns them withfilter().