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.
<!DOCTYPE html>
<html>
<head>
<title>
VueJS | v-show directive
</title>
<!-- Load Vuejs -->
<script src=
"https://cdn.jsdelivr.net/npm/vue/dist/vue.js">
</script>
</head>
<body>
<div style="text-align: center; width: 600px;">
<h1 style="color: green;">
GeeksforGeeks
</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.
<!DOCTYPE html>
<html>
<head>
<title>
VueJS | v-show directive
</title>
<!-- Load Vuejs -->
<script src=
"https://cdn.jsdelivr.net/npm/vue/dist/vue.js">
</script>
</head>
<body>
<div style="text-align: center;width: 600px;">
<h1 style="color: green;">
GeeksforGeeks
</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: