BeginnerCoder

Lesson 9 — Conditional render

v-if and v-else

v-if="condition" shows an element only when the condition is true; an element right after it with v-else shows otherwise. Vue really adds and removes the elements, so only one of the two exists at a time.

<p v-if="cart.length">You have items</p>
<p v-else>Your cart is empty</p>

Why it matters: logged-in vs. logged-out views, empty states and error messages all use this.

Want it another way?
Task

Show "Please log in" at first, and "Welcome back!" after clicking Log in.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.