</>
Skip to content
Vue lessons (14/43)

Vue — Conditional Rendering

v-if

<template>
  <div>
    <p v-if="isLoggedIn">Welcome back!</p>
    <p v-else>Please log in</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoggedIn: false
    }
  }
}
</script>

v-else-if

<div v-if="score >= 90">A</div>
<div v-else-if="score >= 80">B</div>
<div v-else-if="score >= 70">C</div>
<div v-else>F</div>

v-else

<div v-if="items.length">
  <ul>
    <li v-for="item in items">{{ item }}</li>
  </ul>
</div>
<div v-else>No items found</div>

v-show

<div v-show="isVisible">Visible content</div>

v-if vs v-show

Featurev-ifv-show
RenderingLazyAlways rendered
DOMAdded/removedDisplay toggle
PerformanceGood for togglesGood for frequent
TransitionSupportsSupports

Template v-if

<template v-if="isLoggedIn">
  <h1>Dashboard</h1>
  <p>Welcome!</p>
</template>

Multiple root elements

<template v-if="show">
  <header>Header</header>
  <main>Content</main>
</template>

Conditional with v-for

<ul>
  <li v-for="item in items" :key="item.id">
    <span v-if="item.active">{{ item.name }}</span>
  </li>
</ul>

Mini Practice

  1. Toggle elements with v-if
  2. Use v-else-if for multiple conditions
  3. Compare v-if and v-show performance
  4. Conditionally render lists

Up Next

Continue with Lists - Rendering arrays with v-for.

Related Topics

Frequently Asked Questions about Conditional Rendering

What is Conditional Rendering in Vue?

Conditional Rendering is a fundamental concept in Vue. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Conditional Rendering?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Conditional Rendering.

Why is Conditional Rendering important in Vue?

Conditional Rendering is essential for Vue development. Understanding this concept will help you write better code and solve real-world problems more effectively.