Author -  Sai gowtham

How to render elements conditionally in Vue.js

In this tutorial, we are going to learn about conditional rendering in Vue.js.

What is Conditional rendering?

Conditional rendering means to add or remove elements from the dom if a particular condition is true.

In Vue, we need to use v-if directive to render the elements conditionally.

Let’s see an example.

<template>
  <div>
  <!-- v-if="javascript expression" -->   <h1 v-if="isActive">Hello Vuejs</h1>
   <button @click="isActive= !isActive">Click</button>
  </div>
</template>

<script>

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

In the above code we have added a v-if directive with property isActive so that h1 element only render into the dom, if isActive property is true.

We can also extend the v-if directive followed by the v-else directive.

   <h1 v-if="isActive">Hello Vuejs</h1>
   <h1 v-else>Hey Vuejs</h1>

If isActive property is true first h1 element will render otherwise second h1 element will render into the dom.

We can also further extend it using v-else-if block.

   <h1 v-if="isActive">Hello Vuejs</h1>
   <h1 v-else-if="isActive && a.length===0">You're vuejs</h1>
   <h1 v-else>Hey Vuejs</h1>

v-else-if directive works like a else-if block in JavaScript.

Note: A v-else element must immediately follow a v-if element or v-if-else element - otherwise it will not be recognized.

How to render multiple elements conditionally?

Sometimes we have to render multiple elements conditionally in such cases we need to group the elements together.

<template>
  <div v-if="available">    <h1>Items are available</h1>
    <p>More items in the stock</p>
  </div>
  <div v-else>    <h1>Items are not available</h1>
    <p>Out of stock</p>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      available: true
    };
  }
};
</script>

Here we group the multiple elements in a div tag.

render-multiple-items-vuejs

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY