DEV Community

loading...
Cover image for use bootstrap 5 in vue correctly

use bootstrap 5 in vue correctly

tefoh profile image Tefoh ・3 min read

In this post we will see how use bootstrap in vue application. because bootstrap 5 drop support jQuery we can easily use it without an ui framework like bootstrap-vue. first we load bootstrap styles and then we use bootstrap modal to see how bootstrap javascript actually works.

install vue with vitejs

first we install vue application with vitejs. you can use vue-cli if you want but here it doesnt matter. for install vitejs app run this command:

npm init @vitejs/app
Enter fullscreen mode Exit fullscreen mode

meanwhile this will ask you project name and application template. for template select vue. then go project directory, install dependencies and run the vitejs server.

install bootstrap 5

now the vue app installed we should install bootstrap and sass for compile scss files. run this commands:

npm install bootstrap@next
npm install -D sass
Enter fullscreen mode Exit fullscreen mode

now we can load bootstrap styles. in main.js file we can easily load bootstrap.scss like this:

import 'bootstrap/scss/bootstrap.scss'
Enter fullscreen mode Exit fullscreen mode

if we use bootstrap classes for e.x. btn classes like <button class="btn btn-primary">test</button> we will see styles applied.

use bootstrap modal

bootstrap besides the styles give us some javascript that make our life easier and modal is one simple example. for this we will use bootstrap docs modal example. you can use this in any component template.

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

there is two parts here one is the button show the modal and second part is the modal. so if we use this nothing works, why? because we just load styles not bootstrap javacript. so we load it too. with import { Modal } from 'bootstrap' we can load only the bootstrap modal javascript part awesome. but we will get an error that says @popperjs/core package not installed. lets install it with this simple command:

npm i @popperjs/core
Enter fullscreen mode Exit fullscreen mode

bootstrap need this package for javascript part. now modal should works. nice :) but we havent use vue vDOM too make this modal work lets do it with vDOM.

use virtual dom

vue and react use virual dom and because of that we use mostly with state in this frameworks but when we need to use dom elements not state like when want to focus input we should use refs. there is two ways to use bootstrap javascript one using data-bs, two use javascript. with second way we can use refs to make modal works. bootstrap docs says we can use it like this:

var myModal = new Modal(document.getElementById('exampleModal'), options)
Enter fullscreen mode Exit fullscreen mode

Modal is the one we imported earlier. we can replace document.getElementById('exampleModal') with our ref and we will name our ref exampleModal. we will need a state for modal to show and hide the modal.

  data: () => ({
    modal: null
  }),

  mounted() {
    this.modal = new Modal(this.$refs.exampleModal)
  }
Enter fullscreen mode Exit fullscreen mode

with modal.show() we can show modal and for hide just use hide method and the button for show modal will be like this:

<button type="button" class="btn btn-primary" @click="modal.show()">
  Launch demo modal
</button>
Enter fullscreen mode Exit fullscreen mode

and now we use vDOM to use bootstrap modal. good job :)) the hole simple component will be like this:

<template>
  <button type="button" class="btn btn-primary" @click="modal.show()">
    Launch demo modal
  </button>
  <div class="modal fade" ref="exampleModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="btn-close" @click="modal.hide()" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" @click="modal.hide()">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { Modal } from 'bootstrap'
export default {
  name: "App",
  data: () => ({
    modal: null
  }),
  mounted() {
    this.modal = new Modal(this.$refs.exampleModal)
  }
};
</script>
Enter fullscreen mode Exit fullscreen mode

Summary

we created a vitejs vue application and installed bootstrap. then we used bootstrap modal with data-bs and easy way but after that used vue.js vDOM and correct way. i hope this short post helped i covered this topics in my course and shared my little Knowledge about this topic.

Discussion (3)

pic
Editor guide

Some comments have been hidden by the post's author - find out more