The Chicken Noodles at Mrs. Sri's stall are sold out. Mrs. Sri can do one of two things: take the Chicken Noodles card off the wall and hang a "Sold out" card instead, or leave the card hanging and cover it with a cloth.
Vue has the same two ways of doing conditional rendering, showing part of a template only when a condition is true. Taking the card down is like v-if: the element is removed from the page. Covering the card with a cloth is like v-show: the element stays, but it is hidden.
v-if and v-else: pick one of two
Here is the Chicken Noodles menu card, and what it shows depends on stock:
const stock = ref(0)
---template---
<h3>Chicken Noodles</h3>
<p v-if="stock > 0">Available: {{ stock }} portions</p>
<p v-else>Sold out</p>
What goes into the DOM for two values of stock:
stock |
What renders in the DOM |
|---|---|
| 0 | <h3>Chicken Noodles</h3><p>Sold out</p> |
| 5 | <h3>Chicken Noodles</h3><p>Available: 5 portions</p> |
v-ifis a directive, a special Vue attribute starting withv-, just likev-htmlin the template lesson. Its value is a JavaScript expression. If the result is truthy (counts as true), the element goes in. If it's falsy (counts as false, such asfalse,0,"",null,undefined), the element is never created at all.v-elsetakes no value. The element withv-elsegoes in when the condition of thev-ifabove it is false.v-elsemust sit on the element right after thev-ifelement, with no other element between them.- Each choice here, the
v-ifelement or thev-elseelement, is called a branch. Only one branch is ever in the DOM. That's why both branches may use the same id. In the first exercise, you write<p id="msg" v-if="isLoggedIn">Welcome!</p>then<p id="msg" v-else>Please log in.</p>. WithisLoggedInset totrue, onlyWelcome!shows. The#togglebutton in the starter code switchesisLoggedInfromtruetofalse, and back. After that button is clicked,Please log in.shows. - As soon as
stock.valuechanges, Vue checks the condition again and swaps the branch by itself. - The ternary from the template lesson,
{{ stock > 0 ? "In stock" : "Sold out" }}, is enough to swap text.v-ifis for adding or removing elements.
v-else-if: more than two branches
For more than two choices, use v-else-if between the v-if and the v-else:
const stock = ref(3)
---template---
<p v-if="stock >= 10">Plenty left</p>
<p v-else-if="stock > 0">Only {{ stock }} left</p>
<p v-else>Sold out</p>
The branch that goes in for three values of stock:
stock |
The branch that goes in |
|---|---|
| 12 | Plenty left |
| 3 | Only 3 left |
| 0 | Sold out |
- Vue checks from the top. The first branch whose condition is true goes in, and the rest are skipped. With a
stockof 12, only "Plenty left" shows, even though12 > 0is also true. - That's why you write the strictest condition first:
stock >= 10beforestock > 0. - The second exercise uses the same pattern.
scoreholds 85. The paragraph withv-if="score >= 80"holdsA,v-else-if="score >= 60"holdsB, andv-elseholdsC. All three paragraphs useid="grade", and what shows isA. The starter code also has an<input id="score">. If you type65or60there,Bshows. If you type40,Cshows.
One condition for several elements: <template v-if>
When one condition applies to several elements, wrap those elements in a <template v-if>:
const stock = ref(3)
---template---
<template v-if="stock > 0">
<p>Available: {{ stock }} portions</p>
<button>Order</button>
</template>
<p v-else>Sold out</p>
<template>is a wrapper that doesn't go into the DOM itself. Withstockat 3, the DOM holds just the<p>and the<button>, with no wrapper element.
v-if or v-show
This code uses the same condition twice, once with v-if and once with v-show:
const stock = ref(0)
---template---
<p v-if="stock > 0">Available (v-if)</p>
<p v-show="stock > 0">Available (v-show)</p>
The DOM that Vue makes with stock at 0:
<!--v-if-->
<p style="display: none;">Available (v-show)</p>
There is only one <p>, and it belongs to v-show. v-if doesn't create its element at all. Vue only leaves an HTML comment as a placeholder: <!--v-if--> in the development build, and <!----> in the production build like the Playground's. v-show always creates its element, then hides it with the CSS display: none.
v-if |
v-show |
|
|---|---|---|
| Condition is false | the element isn't in the DOM | the element is there, display: none |
| Condition changes | the element is thrown away, then made again | only its display changes |
| Text typed into an input inside | lost | kept |
Paired with v-else or <template> |
yes | no |
| Good for | rarely changes, or only for some people | flips back and forth often, like tabs |
- For example, take an order box holding a note input, and the box only shows while there is stock. With
v-if, type "spicy", sell all the stock, then add stock again. The input is empty, because what shows is a new element. Withv-show, the box was only hidden, so "spicy" is still there. v-showstill creates its element even when it starts hidden. Av-ifwhose condition is false from the start creates nothing.
⚠️ When two branches appear, or none at all
- A
v-elsethat isn't attached. Another element sits between thev-ifand thev-else, or thev-elsefollows av-show. In development mode, the console writes[Vue warn]: Template compilation error: v-else/v-else-if has no adjacent v-if or v-else-if.The Playground uses the production build, so all that appears is an error holding the addresshttps://vuejs.org/error-reference/#compiler-30, and the component doesn't show at all. - Three separate
v-ifs. With a score of 85,v-if="score >= 80",v-if="score >= 60"andv-if="score < 60"showAandB, because eachv-ifis checked on its own. Chain them withv-else-ifandv-elseso only one branch shows. In the exercises, the checker makes sure only one<p id="grade">is on the page. v-showfor a branch that must disappear. Two paragraphs withv-showare both still in the DOM, and one of them is hidden. In the first exercise, the unused branch must really disappear, so usev-ifandv-else.- Forgetting that
v-ifrebuilds. Text typed into an input inside av-ifblock is lost every time the condition becomes false, because the element is thrown away and made again. If the input must stay, usev-show.
TL;DR: v-if="condition" is a directive (a special Vue attribute) that puts an element in only while the condition is true. When the condition is false, the element isn't in the DOM. v-else-if and v-else follow on the next elements in one chain, and each must sit right after the element before it. Of all the branches (choices) in that chain, only the first one whose condition is true goes in. <template v-if> wraps several elements without going into the DOM itself. v-show always creates its element, and while the condition is false it hides it with display: none. So its contents stay, and v-show suits elements that are shown and hidden often. v-show can't be paired with v-else. In short, v-if="false" removes the element, and v-show="false" hides it.
⚡ v-if and v-else: pick one
Write:
const stock = ref(0)
---template---
<h3>Chicken Noodles</h3>
<p v-if="stock > 0">Available: {{ stock }} portions</p>
<p v-else>Sold out</p>
Meaning: v-if is a directive (a special Vue attribute) that puts an element in only if its condition is true. Each choice here, the v-if element or the v-else element, is called a branch. With stock at 0, the condition stock > 0 is false, so of the two <p> elements only <p>Sold out</p> goes into the DOM. A branch whose condition is false is never created at all. v-else takes no value and must sit right after the v-if.
⚡ v-else-if is checked from the top
Write:
<p v-if="stock >= 10">Plenty left</p>
<p v-else-if="stock > 0">Only {{ stock }} left</p>
<p v-else>Sold out</p>
Guess first: stock is 12, so stock >= 10 is true, and 12 > 0 is true as well. How many paragraphs show?
Just one: "Plenty left". The first branch whose condition is true goes in, and the rest are skipped.
Meaning: v-else-if adds a branch between the v-if and the v-else, and Vue checks the conditions from the top. That's why the strictest condition is written first: stock >= 10 before stock > 0.
⚡ v-if removes, v-show hides
Write:
<p v-if="stock > 0">Available (v-if)</p>
<p v-show="stock > 0">Available (v-show)</p>
Guess first: with stock at 0, how many <p> elements are in the DOM?
One, the v-show one, with style="display: none;". v-if doesn't create its element at all.
Meaning: v-if="false" removes the element, v-show="false" hides it with display: none. v-show suits elements that are shown and hidden often, like the content of tabs.