Skip to content
Related Articles

Related Articles

Vue.js | v-else directive
  • Last Updated : 22 Jun, 2020

The v-else directive is a Vue.js directive used to toggle the display CSS property of a element only when the if condition is not satisfied. First, we will create a div element with id as app and let’s apply the v-else directive to a element with data. Now we will create this data by initializing a Vue instance with the data attribute containing the value.

Syntax:

v-else

Parameters: This directive doesn’t accepts any parameters.
Example 1: This example uses VueJS to show an element with v-else using arithmetic conditions.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        VueJS | v-else directive
    </title>
  
    <!-- Load Vuejs -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;width: 600px;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <b>
            VueJS | v-else directive
        </b>
    </div>
  
    <div id="canvas" style="border:1px solid #000000;
                            width: 600px;height: 200px;">
        <div id="app">
            <h2 v-if="a > 10">a is greater than 10</h2>
            <h2 v-else>a is smaller than or equal to 10</h2>
        </div>
    </div>
  
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                a: 9
            }
        })
    </script>
</body>
</html>

chevron_right


Output:



Example 2: This example uses VueJS to show an element with v-else using booleans.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        VueJS | v-else directive
    </title>
  
    <!-- Load Vuejs -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;width: 600px;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <b>
            VueJS | v-else directive
        </b>
    </div>
  
    <div id="canvas" style="border:1px solid #000000;
                            width: 600px;height: 200px;">
        <div id="app">
            <h2 v-if="data">if is executed</h2>
            <h2 v-else>else is executed</h2>
        </div>
    </div>
  
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                data: false
            }
        })
    </script>
</body>
</html>

chevron_right


Output:

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :