Table of contents
1.
Introduction 
2.
Conditional Rendering in Vue
3.
Conditional Rendering in Vue: v-show
4.
Conditional Rendering in Vue: v-if
5.
Conditional Rendering in Vue: v-else
6.
Conditional Rendering in Vue: v-else-if
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Conditional Rendering in Vue

Author Hari Sapna Nair
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Vue.js is an open-source, model–view–viewmodel, and progressive frontend JavaScript framework for building user interfaces. It is an easy-to-learn and approachable library to start building web applications. The focus of the core library is on the view layer and is easy to pick up and integrate with other libraries. It can power sophisticated SPAs (Single-Page Applications) with modern tooling and supporting libraries.

 

Suppose we have a situation while developing a Vue application where we only want to render a specific template when a particular condition is met; this is where conditional rendering in Vue comes into the picture.

 

In this blog, let us discuss conditional rendering in Vue in detail.

Conditional Rendering in Vue

Vue.js comes bundled with some fantastic features. One outstanding feature is conditional rendering. Before discussing Conditional Rendering in Vue, let's understand what Conditional Rendering is.

 

Conditional rendering is the ability to control the presence of any element in the DOM(Document Object Model) based on a particular condition. It allows us to render templates under certain conditions.
 

Conditional rendering plays a vital role in any dynamic web application. Vue.js makes it easy by introducing a directive that we can easily implement to render elements or blocks conditionally. It provides us with different conditional rendering methods. Let us discuss them in detail.

Conditional Rendering in Vue: v-show

The v-show directive disables the visibility of the element and hides it. If the value of the passed variable or expression is false, it hides the element, else displays it. Let us see an example,

 

Code:

<template>
 <div>
  <!-- Displayed as the condition is fullfilled-->
  <h1 v-show = trueCondition>Hello! I am vissible</h1>
  <h1 v-show = falseCondition>I am not vissible</h1>
 </div>
</template>

<script>
export default {
 data() {
   return {
     trueCondition: true,
     falseCondition: false,
   };
 },
};
</script>

 

Output

Conditional Rendering in Vue: v-if

The v-if directive does not hide the element. However, it does not render anything until the value of the passed expression or variable becomes true. For example,

 

Code:

<template>
 <div>
  <!-- Displayed as the condition is fullfilled-->
  <h1 v-if = trueCondition>Hello! I am vissible</h1>
  <h1 v-if = falseCondition>I am not vissible</h1>
 </div>
</template>

<script>
export default {
 data() {
   return {
     trueCondition: true,
     falseCondition: false,
   };
 },
};
</script>

Output
 

The v-if directive ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles.
 

Also, if the condition is false on the initial render, it will not do anything, and the conditional block won't be rendered until the condition becomes true for the first time. It also has higher toggle costs.

Conditional Rendering in Vue: v-else

We can also use the v-else directive and the v-if statement to distinguish between any two blocks conditionally. But, keep in mind that the v-else block must appear right after the v-if block. It is used to render a block that does not fulfill the condition of the v-if directive.
 

Code:

<template>
 <div>
  <h1 v-if = condition> I am not visible.</h1>
  <!-- Displayed as the above condition is not fullfilled-->
  <h1 v-else> I am visible.</h1>
 </div>
</template>

<script>
export default {
 data() {
   return {
     condition: false,
   };
 },
};
</script>

 

Output

 

Note: The v-else directive can also be used along with templates.

Conditional Rendering in Vue: v-else-if

Like the v-else directive, we can also use the v-else-if directive with the v-if directive. If the v-if condition is not fulfilled, the v-else-if condition is checked. For example, 

 

Code:

<div>
    <h1 v-if = "1 === 0">1 === 0</h1>
    <!-- Displayed as the above condition is not fullfilled and the following condition is true-->
    <h1 v-else-if = "1 !== 0">1 !== 0</h1>
</div>

 

Output

Frequently Asked Questions

1. What is the difference between v-show and v-if?

Ans:- An element with the v-show directive will always be rendered and remain in the DOM. This is done by toggling the display CSS property of the element. 

 

In the case of the v-if directive, the element is completely removed from DOM.

 

2. How to hide more than one element?

Ans:- To toggle more than one element, we can use v-if on the <template> element, an invisible wrapper. 

 

3. Why isn't it recommended to use v-for along with v-if?

Ans:- It isn't recommended to use v-for along with v-if as v-for has higher priority than v-if when they're used together. And Vue has to check each element for v-if's condition in every iteration.

 

4. What are the use cases of conditional rendering in Vue?

Ans:- Some of the use cases of conditional rendering in Vue are as follows:-

  • Toggling between the application
  • Show or hide blocks
  • Implement access permissions

 

5. When should we use v-show and v-if?

Ans:- The v-if directive has a higher toggle cost, while the v-show directive has a higher initial render cost. Hence, one should prefer the v-show directive if we need to toggle something often and prefer the v-if directive if the condition is unlikely to change at runtime.

Key Takeaways

This blog covered the basic concepts of  Conditional Rendering in Vue in detail and various examples and frequently asked questions. We learned how to render the DOM in Vue.js using directives conditionally.

 

Don't stop here. Check out our Learn Vue free course to learn Vue from scratch. Also, feel free to check out the blog Vue JS Vs. React in 2021.

 

We hope you found this blog useful. Liked the blog? Then feel free to upvote and share it.

Live masterclass