How to Add and Use Bootstrap Modal in Vue JS App

Vue js bootstrap modal example tutorial. In this tutorial, you will learn how to use bootstrap modal in vue js or How to open bootstrap modal in VueJS.

This tutorial will guide you step by step on how to use or open bootstrap modal in vue js. And as well as, how to render content on bootstrap modal.

Bootstrap modals are lightweight and multi-purpose popups. Modals are split into three primary sections: header, body, and footer. Each has its role and so should be used accordingly.

  • Modals are built with HTML, CSS, and JavaScript. They’re positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead.
  • Clicking on the modal “backdrop” will automatically close the modal.
  • Bootstrap only supports one modal window at a time. Nested modals aren’t supported as we believe them to be poor user experiences.
  • Modals use position: fixed, which can sometimes be a bit particular about its rendering. Whenever possible, place your modal HTML in a top-level position to avoid potential interference from other elements. You’ll likely run into issues when nesting a .modal within another fixed element.
  • Due to how HTML5 defines its semantics, the autofocus HTML attribute has no effect in Bootstrap modals. To achieve the same effect, use some custom JavaScript.

How to Use Bootstrap Modal in Vue JS

This tutorial will show you multiple examples of how to use bootstrap modal in vue js:

Example 1 – Bootstrap Modal in Vue JS

<!DOCTYPE html>
<html>
<head>
   <title>Vue.Js Bootstrap Modal Example Tutorial - Tutsmake.com</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>
   <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body class="bg-dark">
   <div class="container">
      <div class="col-md-12 mt-4" id="components-demo">
         <div class="row mt-3">
            <div class="col-md-5 offset-md-3 p-3 shadow-lg" style="border:2px solid red;">
               <h5 class="text-white">Vue Js Bootstrap Modal Example - Tutsmake.com</h5>
               <button type="button" class="btn btn-primary" @click="showModel = true" >
                 Open modal
               </button>
               <model v-if="showModel" @close="showModel = false">
                  <p>tutsmake.com</p>
               </model>
            </div>
         </div>
      </div>
   </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
Vue.component('model', {
  template: `<!-- The Modal -->
          <div class="modal" id="myModal" style="display: block;">
            <div class="modal-dialog">
              <div class="modal-content">
                <!-- Modal Header -->
                <div class="modal-header">
                  <h4 class="modal-title">Modal Heading</h4&
                  <button type="button" class="close" @click="$emit('close')" data-dismiss="modal">×</button>
                </div>
                <!-- Modal body -->
                <div class="modal-body">
                  <slot></slot>
                </div>
                <!-- Modal footer -->
                <div class="modal-footer">
                  <button type="button" @click="$emit('close')" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
              </div>
            </div>
          </div>`
});

new Vue({ el: '#components-demo',
  data:{
    showModel:false
  }
})
</script>
</body>
</html>   

The following code is used to show bootstrap modal in vue js:

<script>
  Vue.component('model', {
    template: `<!-- The Modal -->
            <div class="modal" id="myModal" style="display: block;">
              <div class="modal-dialog">
                <div class="modal-content">
                  <!-- Modal Header -->
                  <div class="modal-header">
                    <h4 class="modal-title">Vue.Js Bootstrap Modal Example</h4>
                    <button type="button" class="close" @click="$emit('close')" data-dismiss="modal">×</button>
                  </div>
                  <!-- Modal body -->
                  <div class="modal-body">
                    <slot></slot>
                  </div>
                  <!-- Modal footer -->
                  <div class="modal-footer">
                    <button type="button" @click="$emit('close')" class="btn btn-danger" data-dismiss="modal">Close</button>
                  </div>
                </div>
              </div>
            </div>
              `
  });

  new Vue({ el: '#components-demo',
    data:{
      showModel:false
    }
  })
</script>

Conclusion

vue js bootstrap modal example tutorial, you have learn how to use bootstrap modal in vue js.

Recommended Vue JS Tutorials

AuthorDevendra Dode

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 *