3

main.js

import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js";

Component.vue

<template> 
                  <i
                class="fas fa-info-circle"
                data-bs-toggle="tooltip"
                data-bs-placement="top"
                title="Tooltip on top"
                ref="info"
              ></i>


</template>
<script>
import { Tooltip } from "bootstrap/dist/js/bootstrap.esm.min.js";

export default {
  mounted() {
    console.log(this.$refs.info);
    new Tooltip(this.$refs.info)
  },
}
</script>

I tried clicking on the icon but nothing is coming out. Am I doing this wrongly?

8 Answers 8

21

I've had the same problem. This code works for me.

import { Tooltip } from 'bootstrap'

export default {
  mounted() {
    new Tooltip(document.body, {
      selector: "[data-bs-toggle='tooltip']",
    })
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this thread works for me, thx a lot :)
14

You can create a directive "v-tooltip" and used in any element html.

create file tooltip.js

import { Tooltip } from 'bootstrap'

export const tooltip = {
    
  mounted(el) {
    const tooltip = new Tooltip(el)
  }

}

In main.js add

import { tooltip } from 'your_path/tooltip'

app.directive('tooltip', tooltip)

and use it:

<button v-tooltip title="Hello world!">
    I am a button
</button>

5 Comments

I'm not an expert in frontend, been struggling for hours now. This solution is so simple even I can follow. Thank you, it works.
Simple and efficient, thanks a lot.
This is great. The only issue I have with this is that the Tooltip doesn't go away when the element does.
You can add this (simplified to shortcode): unmounted: ( element ) => try { element.tooltip?.bsInstance?.dispose?.(); } catch { /* noop */ }
I must add, I am having a more reactive, less buggy experience if I set the data-bs-original-title attribute directly and the title not at all: <button v-tooltip :data-bs-original-title=".tooltip">etc. For example, you can see the improvement better if you have a language switcher.
9

Here is a more elegant and modern solution that uses Typescript with the new Vue 3 script setup.

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { Tooltip } from "bootstrap";

const info = ref(null);
let infoTooltip: Tooltip | undefined;

onMounted(() => {
  infoTooltip = new Tooltip(info.value!);
});
</script>

<template> 
  <i
    class="fas fa-info-circle"
    data-bs-toggle="tooltip"
    data-bs-placement="top"
    title="Tooltip on top"
    ref="info"
  ></i>
</template>

1 Comment

This should be the accepted answer as the Tooltip is tied to the element.
3

Simply use import { Tooltip } from "bootstrap";, and it should work.

Demo

1 Comment

For me, this worked. Adding data-bs-toggle="tooltip" data-bs-placement="top" title="My tooltip." to the container was all that was needed (no js). One interesting thing on Mac is that data-bs-placement="top" doesn't place the tooltip on top. Need to figure that out.
3

I just added this code from Kyanoush at the App.Vue and it works great:

import { Tooltip } from 'bootstrap'

export default {
  mounted() {
    new Tooltip(document.body, {
      selector: "[data-bs-toggle='tooltip']",
    })
  }
}

Comments

0

If you are using bootstrap CDN then you can also do it in App.vue mounted function like this :

<script>

export default {
  mounted() {
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
      return new bootstrap.Tooltip(tooltipTriggerEl)
    });
  }
}

</script>

Comments

0

For usage in one component context only

<script setup>
  import {Tooltip} from "bootstrap"

  const vCustomTooltip = {
    mounted: (el) => new Tooltip(el)
  }
</script>
<template>
   <div class="wrapper">
     <div v-custom-tooltip title="Email verification"></div>
     <div v-custom-tooltip title="Phone verification"></div>
   </div>
</template>
  • Tooltip directive (vCustomTooltip) can be named whatever
  • Default settings for bootstrap tooltip is used (placement top, title as tooltip value)

Comments

0

for those who use cdn or direct link to bootstrap file use this code

export const tooltip = {

    mounted(el) {
        const tooltip = new bootstrap.Tooltip(el)
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.