如何在Nuxt中使用和初始化Bootstrap 5的工具提示(data-bs-toggle=”tooltip”)?

huangapple go评论56阅读模式
英文:

How to use and initialize bootstrap5 tooltip in nuxt (data-bs-toggle="tooltip" )?

问题

The code you provided is meant to initialize Bootstrap tooltips in a Nuxt.js project. However, it seems like there might be an issue with the jQuery dependency. To make it work with Bootstrap 5 and Nuxt.js, you can modify the code like this:

import Vue from 'vue';
import 'bootstrap/dist/js/bootstrap.bundle.min.js'; // Include Bootstrap JavaScript

const bsTooltip = (el, binding) => {
    const t = [];

    if (binding.modifiers.focus) t.push('focus');
    if (binding.modifiers.hover) t.push('hover');
    if (binding.modifiers.click) t.push('click');
    if (!t.length) t.push('hover');

    // Use vanilla JavaScript to initialize tooltips
    new bootstrap.Tooltip(el, {
        title: binding.value,
        placement: binding.arg || 'top',
        trigger: t.join(' '),
        html: !!binding.modifiers.html,
    });
};

Vue.directive('tooltip', {
    bind: bsTooltip,
    update: bsTooltip,
    unbind(el) {
        // Dispose of the tooltip when the element is unbound
        const tooltip = bootstrap.Tooltip.getInstance(el);
        if (tooltip) {
            tooltip.dispose();
        }
    },
});

Make sure you've imported Bootstrap's JavaScript (bootstrap.bundle.min.js) properly in your project. This code should work with Bootstrap 5 and Nuxt.js.

英文:

Bootstrap 5 documentation says I have to add this code to initialize tooltips.

const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))

How should this be done properly in nuxt so the tooltips are initialized and can be used globally?


the nearest thing I found was in an answer to this question: https://stackoverflow.com/questions/37078423/how-can-add-bootstrap-tooltip-inside-vue-js

where it states to do by creating a bs-tooltips.js file in the plugin folder with the following code:

import Vue from 'vue'

const bsTooltip = (el, binding) => {
    const t = []

    if (binding.modifiers.focus) t.push('focus')
    if (binding.modifiers.hover) t.push('hover')
    if (binding.modifiers.click) t.push('click')
    if (!t.length) t.push('hover')

    $(el).tooltip({
        title: binding.value,
        placement: binding.arg || 'top',
        trigger: t.join(' '),
        html: !!binding.modifiers.html,
    });
}

Vue.directive('tooltip', {
    bind: bsTooltip,
    update: bsTooltip,
    unbind (el) {
        $(el).tooltip('dispose')
    }
});

then add this to the nuxt config file:

plugins: [
    '~/plugins/bs-tooltips.js'
],

and then this should work:

<button v-tooltip="'Tooltip text'">Hover me</button>
<button v-tooltip.click="Tooltip text">Click me</button>
<button v-tooltip.html="Tooltip text">Html</button>
<button v-tooltip:bottom="Tooltip text">Bottom</button>
<button v-tooltip:auto="Tooltip text">Auto</button>

but that throws following ERROR: $ is not defined

My guess is that this is probably meant for bootstrap 4 / jQuery and probably an older version of nuxt (the answer is from 2018)

Has anyone integrated this feature and bootstrap 5 in nuxt 2?
Is interesting if it works as a directive of course, but I'm inclined to just have it work as the documentation of bootstrap states, with data-bs-toggle="tooltip" and data-bs-title attributes

答案1

得分: 0

以下是翻译好的部分:

老的插件集成方法有效,但需要以下代码:

我在我的插件文件夹中创建了 "bs-tooltips.js" 文件,然后将以下代码放入其中:

export default function ({ app }) {
  if (process.client) {
    window.onload = () => {
      const tooltips = document.querySelectorAll('[data-bs-toggle="tooltip"]');
      tooltips.forEach((tooltip) => {
        if (!tooltip._tooltip) {
          new window.bootstrap.Tooltip(tooltip);
        }
      });
    };
  }
}

然后将这段代码添加到 Nuxt 配置文件中:

plugins: [
    '~/plugins/bs-tooltips.js'
],

之后,下面的代码应该按预期工作:

<a href="#" data-bs-toggle="tooltip" data-bs-title="默认工具提示">内联链接</a>

注意: 请确保在您的 Nuxt.js 应用程序中正确集成了 Bootstrap 本身,使用了 bootstrap.bundle.min.js 或 popper,并且所有下拉菜单和常用的 Bootstrap 组件都已按预期工作。

英文:

the old approach of integrating as a plugin works, but with the following code:

I created the bs-tooltips.js file in my plugins folder, then put the following code into that:

export default function ({ app }) {
  if (process.client) {
    window.onload = () =&gt; {
      const tooltips = document.querySelectorAll(&#39;[data-bs-toggle=&quot;tooltip&quot;]&#39;);
      tooltips.forEach((tooltip) =&gt; {
        if (!tooltip._tooltip) {
          new window.bootstrap.Tooltip(tooltip);
        }
      });
    };
  }
}

then add this to the nuxt config file:

plugins: [
    &#39;~/plugins/bs-tooltips.js&#39;
],

after that:

&lt;a href=&quot;#&quot; data-bs-toggle=&quot;tooltip&quot; data-bs-title=&quot;Default tooltip&quot;&gt;inline links&lt;/a&gt;

should work as expected

NOTE: Be Sure that bootstrap itself with the bootrap.bundle.min.js or popper is properly integrated into your nuxt.js application and all dropdowns and useal bootstrap components are already working as expected

huangapple
  • 本文由 发表于 2023年5月10日 20:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定