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.
<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.
<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 usev-if="items.length"orv-if="items.length > 0", notv-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.
<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).
<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:
<!-- ❌ 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-ifis ideal: nothing is rendered until it is needed. If you toggle something many times per second, look atv-showin the next lesson.
Test yourself
What happens to an element when its v-if condition becomes false?
- It gets
display: none - It is removed from the DOM
- It becomes transparent
- It is disabled
Where must a v-else element be placed?
- Anywhere in the same component
- Inside the
v-ifelement - Immediately after a
v-iforv-else-ifelement - Before the
v-ifelement
Which element lets you toggle several siblings with one v-if without rendering a wrapper?
<template><div><span><slot>