Vue Js If Else Condition Example

Vue js if else (v-if-else) example; Through this tutorial, i am going to show you how to use if else or v-if-else in vue js.

In this tutorial, i will take many example to render items conditionally on the screen with Vue.js by using the v-if , v-else-if and v-else directives.

How to use If else or v-if in vue js

This tutorial will show you two examples of how to use if else or v-if-else in vue js:

Example 1 – v-if directive

See the following example of v-if directive:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Js If Else Conditionally Example</title>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="bg-dark">
  <div class="container">
    <div class="col-md-6 offset-md-3">
      <div class="card mt-5">
        <div class="card-header">
          <h5 v-bind:class="red">Vue Js If Conditionally Example</h5>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-md-12">
                <div v-if="show">
                  Hi
                </div>
                        
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data:{
      show: true,
    }
  })
</script>
</body>
</html>

The following code will print items from list of array in vue js:

<div v-if="show">
        Hi
      </div>

Example 2 – v-if-else directive

See the following example of v-if-else directive:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Js If Else Conditionally Example</title>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="bg-dark">
  <div class="container">
    <div class="col-md-6 offset-md-3">
      <div class="card mt-5">
        <div class="card-header">
          <h5 v-bind:class="red">Vue Js If Else Conditionally Example</h5>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-md-12">
              <div v-if="show">
                    <span class="bg-success p-1 text-white">This is show.</span>
                </div>
                <div v-else>
                    <span class="bg-danger p-1 text-white">This is not show.</span>
                </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data:{
      show: true,
    }
  })
</script>
</body>
</html>

The following code will print items from list of array in vue js:

            <div class="col-md-12">
              <div v-if="show">
                    <span class="bg-success p-1 text-white">This is show.</span>
                </div>
                <div v-else>
                    <span class="bg-danger p-1 text-white">This is not show.</span>
                </div>
            </div>

Example 3 – v-else-if directive

See the following example of v-else-if directive:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Js Else If Conditional Example</title>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="bg-dark">
  <div class="container">
    <div class="col-md-6 offset-md-3">
      <div class="card mt-5">
        <div class="card-header">
          <h5 v-bind:class="red">Vue Js Else If Conditional Example</h5>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-md-12">
              <p v-if="val < 10">Val is less than 10</p>
              <p v-else-if="val > 10 && val < 20">val is between 10 and 20</p>
              <p v-else>Pretty high val!!</p>
                        
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data: {
        message: "Hi",
        val: 5
    }
  })
</script>
</body>
</html>

The following code will print items from list of array in vue js:

              <p v-if="val < 10">Val is less than 10</p>
              <p v-else-if="val > 10 && val < 20">val is between 10 and 20</p>
              <p v-else>Pretty high val!!</p>

Conclusion

Vue js if else example tutorial; you have learn how to if else or v-if-else in vue js.

Recommended Vue Js Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.