Table of Contents

  1. Introduction
  2. v-if
  3. v-else
  4. Using v-if on template
  5. v-else-if Multiple conditions
  6. Difference between v-if and v-show
    1. v-if
    2. v-else
    3. Choosing between v-if and v-show

Introduction

To conditionally render the block, Vue uses the v-if directive. In this article, we will look at how to use v-if directive in Vue with examples.

v-if

<span v-if="true">Hey there!</span>

If the expression assigned to v-if returns a truthy value, the block gets rendered in DOM.

<!DOCTYPE html>
<html>
<head>
	<title>Conditional vue</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<p v-if="lorem">
			Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
			tempor incididunt ut labore et dolore magna aliqua.
		</p>
	</div>

	<script>
		var app = new Vue({
			el: "#app",
			data: {
				lorem: true
			}
		})
	</script>
</body>
</html>

In the above example, if the value of ‘lorem’ is set to false, the <p> will not be rendered.

v-if false example vuejs

v-else

Vue also provides us with v-else directive which renders the block if v-if expression returns false. Remember v-else should also be declared immediately after v-if.

<!DOCTYPE html>
<html>
<head>
	<title>Conditional vue</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	<div id="app">
		<span v-if="textCheck">v-if is false</span>
		<span v-else>Rendered using v-else</span>
	</div>

	<script>
		var app = new Vue({
			el: "#app",
			data: {
				textCheck: false
			}
		})
	</script>
</body>
</html>

v-else example false expression vuejs

Using v-if on template

You can also use v-if on a template to apply condition on group of elements.

<!DOCTYPE html>
<html>
<head>
	<title>Vue if/else</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	
	<div id="app">
		<template v-if="display">
			<div>
				<h1>Hello</h1>
				<p>Paragraph 1</p>
				<p>Paragraph 2</p>
			</div>
		</template>
	</div>

	<script>
		new Vue({
			el: "#app",
			data: {
				display: true
			}
		})
	</script>
</body>
</html>

v-if on template true value example vuejs

When the condition is false:

v-if on template false example vuejs

v-else-if Multiple conditions

From version 2.1.0+ we can define “else if” block. You can use one or more v-else-if directives to handle multiple conditions.

<div id="app">
		<h1 v-if="display === 1">1</h1>
		<h1 v-else-if="display === 2">2</h1>
		<h1 v-else-if="display === 3">3</h1>
		<h1 v-else-if="display === 4">4</h1>
		<h1 v-else-if="display === 5">5</h1>
		<h1 v-else>6</h1>
	</div>

	<script>
		new Vue({
			el: "#app",
			data: {
				display: 6
			}
		})
	</script>

Difference between v-if and v-show

Both v-if and v-show are different from each other in the way they work. The v-show directive uses the CSS property ‘display’ to show or hide the content whereas, v-if directive renders the content in the DOM if the expression is evaluated to true. If the expression of v-if results in false the content is not rendered in DOM.

v-show

<div id="app">
		<h1 v-show="display">Cool header</h1>
		
	</div>

	<script>
		new Vue({
			el: "#app",
			data: {
				display: true
			}
		})
	</script>

difference between v-if v-show example

<div id="app">
		<h1 v-show="display">Cool header</h1>
		
	</div>

	<script>
		new Vue({
			el: "#app",
			data: {
				display: false
			}
		})
	</script>

difference between v-if v-show display none example

v-if

<!DOCTYPE html>
<html>
<head>
	<title>Conditional vue</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
	
	<div id="app">
		<h1 v-if="display">Cool header</h1>
		
	</div>

	<script>
		new Vue({
			el: "#app",
			data: {
				display: false
			}
		})
	</script>
</body>
</html>

v-if example and difference between v-show and v-if

Choosing between v-if and v-show

If you need to perform one or the other operation depending on the expression value, use v-if. That is if you need to use v-else if the v-if evaluates to false it makes sense in such a scenario to use v-if. For example, if the user is signed in, display a logout button or else display a login button.

The v-show directive simply displays or hides the element using CSS property. Think of a scenario where you might use a “display: none” property, use v-show instead. For example, hide a task from the listed if it is completed.