Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptVue.js | v-show directive

Vue.js | v-show directive

The v-show directive is a Vue.js directive used to toggle the display CSS property of a element with our data via inline styles. If the data is true it will make it visible else it will make it invisible. First, we will create a div element with id as app and let’s apply the v-show directive to this element with data as a message. Now we will create this message by initializing a Vue instance with the data attribute containing either true or false.

It also triggers transitions when its condition changes
Syntax:

v-show="data"

Parameters: This directive accepts a single parameter which is the data. 
Example 1: This example uses VueJS to show an element with v-show.

HTML




<!DOCTYPE html>
<html>
    <head>
        <title>
            VueJS | v-show directive
        </title>
 
        <!-- Load Vuejs -->
        <script src=
        </script>
    </head>
 
    <body>
        <div style="text-align: center; width: 600px;">
            <h1 style="color: green;">
                neveropen
            </h1>
            <b>
                VueJS | v-show directive
            </b>
        </div>
 
        <div id="canvas" style="border: 1px solid #000000;
                                width: 600px; height: 200px;">
            <div v-show="message" id="app">
                Hello I'm visible.
            </div>
        </div>
 
        <script>
            var app = new Vue({
                el: "#app",
                data: {
                    message: true,
                },
            });
        </script>
    </body>
</html>


Output:

Example 2: This example uses VueJS to hide an element with v-show.

HTML




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


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments