如何在reactjs组件中使用bootstrap 5 popover

发布于 2025-01-16 15:03:19 字数 383 浏览 3 评论 0原文

我看过很多视频,人们

var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
  return new bootstrap.Popover(popoverTriggerEl)
})

仅使用 HTML 文件激活 Bootstrap 5 弹出窗口。我还没有找到任何博客或视频,人们在reactjs 文件中激活弹出窗口。请给我推荐一些例子……由于这个弹出窗口,我真的陷入了给定的任务中。请有人给我推荐一个很好的例子。

I have seen many videos where people activate bootstrap 5 popovers using

var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
  return new bootstrap.Popover(popoverTriggerEl)
})

in just HTML file. I haven't found any blogs or videos where people activate the popover in reactjs files. Please suggest me some examples..I am really stuck in my given task because of this popover. please someone suggest me a good example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2025-01-23 15:03:19

我知道这篇文章已经晚了几个月了,但我也很难找到这样的例子。几个小时后我终于弄清楚了。

这是我发现有效的:
使用 npm install bootstrap 安装 bootstrap

在您的 Index.js 文件中包括:

import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.bundle.min';

在您想要使用弹出窗口的页面或组件中包括:

import { Popover } from 'bootstrap/dist/js/bootstrap.esm.min.js';

使用 useEffect 包括:

useEffect(() => {
    Array.from(document.querySelectorAll('button[data-bs-toggle="popover"]'))
      .forEach(popoverNode => new Popover(popoverNode))
})

最后在您的返回中使用将触发弹出窗口的任何内容:

<button type="button" 
        class="btn btn-dark me-2" 
        data-bs-container="body" 
        data-bs-toggle="popover" 
        data-bs-placement="left" 
        title="I am popover"
        data-bs-content="Left popover">
              Popover on left
</button>

我在 codepen 中有一个工作示例,但不要简单地从 codepen 复制/粘贴,因为它在导入时遵循不同的规则:

Popover Snippet

对我有帮助的资源:

如何导入 Popover

类似的 StackOverflow 问题

Bootstrap 5 Popover 文档

I know this post is a couple months behind, but I was also having a hard time finding a example of this. After a couple of hours I ended up figuring it out.

Here is what I found to work:
install bootstrap with npm install bootstrap

In your Index.js file include:

import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.bundle.min';

In the page or component you want to use the popover include:

import { Popover } from 'bootstrap/dist/js/bootstrap.esm.min.js';

With useEffect include:

useEffect(() => {
    Array.from(document.querySelectorAll('button[data-bs-toggle="popover"]'))
      .forEach(popoverNode => new Popover(popoverNode))
})

And finally in your return use whatever will trigger the popover:

<button type="button" 
        class="btn btn-dark me-2" 
        data-bs-container="body" 
        data-bs-toggle="popover" 
        data-bs-placement="left" 
        title="I am popover"
        data-bs-content="Left popover">
              Popover on left
</button>

I have a working example in codepen but do not simply copy/paste from codepen because it follows a different rule when it comes to importing:

Popover Snippet

Resources that helped me:

How to import Popover

Similar StackOverflow Question

Bootstrap 5 Popover Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文