Conditional Rendering — Vue

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…

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-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

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>

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

⚠️ When two branches appear, or none at all

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.

What you will learn