TExcelTAD
Home › Vue › Vue v-if

Vue v-if

v-if renders an element only when its expression is truthy. When the expression becomes falsy, Vue removes the element from the DOM completely; when it becomes truthy again, Vue creates it again. This is called conditional rendering.

html
<p v-if="isLoggedIn">Welcome back!</p>

v-if, v-else-if and v-else

Chain conditions just like if / else if / else in JavaScript. The v-else-if and v-else elements must come immediately after a v-if or v-else-if element — no other element may sit between them.

html · try it yourself
<style>
  .box { padding: 10px; border-radius: 6px; margin-top: 8px; font-family: sans-serif; }
  .cold { background: #dbeafe; } .ok { background: #dcfce7; } .hot { background: #fee2e2; }
</style>

<div id="app">
  <label>Temperature: {{ temp }} °C
    <input type="range" min="-10" max="45" v-model.number="temp">
  </label>

  <div v-if="temp < 5" class="box cold">🥶 Freezing — wear a coat.</div>
  <div v-else-if="temp < 18" class="box cold">🧥 A bit chilly — bring a jacket.</div>
  <div v-else-if="temp < 30" class="box ok">😎 Perfect weather.</div>
  <div v-else class="box hot">🔥 Hot! Stay in the shade.</div>
</div>

<script type="module">
  import { createApp } from 'vue';

  createApp({
    data: () => ({ temp: 20 })
  }).mount('#app');
</script>

Drag the slider and watch exactly one message appear at a time.

Truthy and falsy

v-if follows JavaScript's rules, so be careful with values like 0 and empty strings:

Value Rendered?
true, 1, 'text', [], {} ✅ yes
false, 0, '', null, undefined, NaN ❌ no

Note: An empty array [] is truthy. To check whether a list has items use v-if="items.length" or v-if="items.length > 0", not v-if="items".

Grouping with <template>

To toggle several elements with one condition without adding an extra wrapper <div>, put v-if on a <template> element. The <template> itself is not rendered — only its children are.

html · try it yourself
<div id="app">
  <button @click="loggedIn = !loggedIn">{{ loggedIn ? 'Log out' : 'Log in' }}</button>

  <template v-if="loggedIn">
    <h3>Hello, {{ user }}!</h3>
    <p>You have {{ messages }} new messages.</p>
  </template>
  <template v-else>
    <h3>Please log in</h3>
    <p>Guests can only browse the catalogue.</p>
  </template>
</div>

<script type="module">
  import { createApp } from 'vue';

  createApp({
    data: () => ({ loggedIn: false, user: 'Minh', messages: 3 })
  }).mount('#app');
</script>

v-if really destroys elements

Because v-if removes the element, anything inside it is reset when it comes back. Type something into the input, hide it, show it again — the text is gone. Components inside are also unmounted and mounted again (you will see their lifecycle hooks run later in the course).

html · try it yourself
<div id="app">
  <label><input type="checkbox" v-model="show"> Show the note field</label>
  <div v-if="show">
    <textarea placeholder="Type a note, then hide and show me"></textarea>
  </div>
  <p>The textarea was created {{ created }} time(s).</p>
</div>

<script type="module">
  import { createApp } from 'vue';

  createApp({
    data: () => ({ show: true, created: 1 }),
    watch: {
      show(value) {
        if (value) this.created++;
        console.log(value ? 'textarea created' : 'textarea removed');
      }
    }
  }).mount('#app');
</script>

(The text in the textarea is not bound to data, so it lives only in the DOM element — and is lost when that element is destroyed. If you bound it with v-model, the value would survive in your data.)

v-if with v-for

Do not put v-if and v-for on the same element. In Vue 3, v-if is evaluated first, so it cannot see the loop variable. Wrap one of them in a <template> instead, or filter the list with a computed property:

html
<!-- ❌ avoid -->
<li v-for="todo in todos" v-if="!todo.done">{{ todo.text }}</li>

<!-- ✅ do this -->
<template v-for="todo in todos" :key="todo.id">
  <li v-if="!todo.done">{{ todo.text }}</li>
</template>

Tip: If the condition rarely changes, v-if is ideal: nothing is rendered until it is needed. If you toggle something many times per second, look at v-show in the next lesson.

Test yourself

What happens to an element when its v-if condition becomes false?

  1. It gets display: none
  2. It is removed from the DOM
  3. It becomes transparent
  4. It is disabled

Where must a v-else element be placed?

  1. Anywhere in the same component
  2. Inside the v-if element
  3. Immediately after a v-if or v-else-if element
  4. Before the v-if element

Which element lets you toggle several siblings with one v-if without rendering a wrapper?

  1. <template>
  2. <div>
  3. <span>
  4. <slot>