Vue.js | v-else directive
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
<!DOCTYPE html>
< html >
< head >
< title >
VueJS | v-else directive
</ title >
< 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 >
|
Output:
Example 2: This example uses VueJS to show an element with v-else using booleans.
HTML
<!DOCTYPE html>
< html >
< head >
< title >
VueJS | v-else directive
</ title >
< 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 >
|
Output:
Last Updated :
22 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...