Vue Js If Else Condition Tutorial

Vue Js If Else Condition Tutorial

Vue js if else (v-if-else) example tutorial. In this tutorial, you will learn how to use if else or v-if-else in vue js.

This tutorial will guide you step by step on how to use if-else or v-if in Vue js. And as well as, The directive v-if is used to conditionally render a block. The block will only be rendered if the directive’s expression returns a truthy value.and You can use the v-else directive to indicate an “else block” for v-if.

The ability to show or hide elements based on conditions is a fundamental feature of any frontend framework. Vue.js provides us with a set of core directives to achieve this effect: v-if, v-else, v-else-if and v-show.

In this example, you will look at various ways 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.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *