【问题标题】:Bootstrap 5 - Uncaught TypeError: Popper__namespace.createPopper is not a functionBootstrap 5 - 未捕获的类型错误:Popper__namespace.createPopper 不是函数
【发布时间】:2021-10-15 09:45:47
【问题描述】:

所以我使用 Django 来提供网页服务。

  • 我正在努力确保我能够下载所有必要的资源,以使网页按预期的方式运行以供离线使用。其中之一是引导程序 5。

  • 我正在尝试使用他们的下拉菜单功能...根据他们的文档:"Dropdowns are built on a third party library, Popper, which provides dynamic positioning and viewport detection. Be sure to include popper.min.js before Bootstrap’s JavaScript or use bootstrap.bundle.min.js / bootstrap.bundle.js which contains Popper. Popper isn’t used to position dropdowns in navbars though as dynamic positioning isn’t required."

  • 因为我非常渴望找到解决方案,所以我插入了一堆 <script> 标签试图排除问题......也许这是个坏主意。

说了这么多,我还没弄明白。帮助:)?

请告诉我如何改进这个问题。提前谢谢!


{% load static %}

<html class="w-100 h-100">
<head>
    <title>
        Fonts
    </title>

    <!--Important meta tags-->
    <!--Cancel zoom for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <!--example:-->
    <!--href="{% static 'polls/style.css' %}"-->

    <!--theme-->
    <link rel="shortcut icon" type="image/jpg" href="{% static 'Pages/favicons/CES.png' %}"/>
    <link type="text/css" rel="stylesheet" href="{% static 'Pages/css/main.css' %}">
    <script src="{% static 'Pages/js/main.js' %}"></script>

    <!--jQuery-->
    <script src="{% static 'Pages/jQuery/jquery-3.6.0.js' %}"></script>
    <script src="{% static 'Pages/jQuery/jquery-3.6.0.min.js' %}"></script>
    <script src="{% static 'Pages/jQuery/jquery-3.6.0.slim.js' %}"></script>
    <script src="{% static 'Pages/jQuery/jquery-3.6.0.slim.min.js' %}"></script>

    <!--bootstrap related-->
    <link type="text/css" rel="stylesheet" href="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/css/bootstrap.css' %}">
    <link type="text/css" rel="stylesheet" href="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.bundle.js' %}"></script>
    <script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.bundle.min.js' %}"></script>
    <script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.js' %}"></script>
    <script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.min.js' %}"></script>

    <style>
{% if fonts %}
{% for font in fonts %}
        @font-face
        {
{% if font.font_family %}
            font-family: "{{font.font_family}}";
{% endif %}
{% if font.path %}
            src: url("{{font.path}}");
{% endif %}
        }
{% endfor %}
{% endif %}
    </style>

</head>
<body class="m-0 p-0 w-100 h-100">

    <button id="fonts" class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown"  aria-expanded="false">
        <span class="visually-hidden">Fonts</span>
    </button>
    
    <ul class="dropdown-menu" role="menu" aria-labelledby="fonts">
{% if fonts%}
{% for font in fonts %}
        <li><div class="dropdown-item" style="font-family: '{{font.font_family}}'; text-align: center;">{{font.font_name}}</div></li>
{% endfor %}
{% endif %}
    </ul>

</body>
</html>

进一步阅读(来自http://5.9.10.113/65685854/dropdown-button-in-bootstrap-5-not-working)表明,实际上是新版本的 Popper 导致了问题......所以......我将进行更多调查,看看我能做些什么。

编辑答案:

仅出于测试目的,我从 bootstrap 的下载页面添加了这个 sn-p,它按预期工作。从这里我浏览了我所有的 bootstrap 资源(更不用说我折叠并添加了 Popper CDN),然后发现我的 bootstrap.js 文件有问题。从 npm 获取它的最新发行版(并将构建放入子目录供我使用)后,我也通过 npm 将 Popper CDN 交换为对应的(我下载了它,进入 node_modules/@popper/core/... ,找到最小文件并引用它)现在它就像一个魅力......最后值得一提的是:只使用 .MIN. 文件!* 如此流行。 min.js、bootstrap.min.js、bootstrap.min.css...在script 标签中包含所有其他文件将导致引导程序无法正常运行。

<html class="w-100 h-100">
<head>
    <title>
        Menu
    </title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>


</head>
<body class="m-0 p-0 w-100 h-100">
  <div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
      Dropdown button
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
  </div>
</body>
</html>

【问题讨论】:

  • 插入所有这些脚本标签是完全错误的。您应该导入 Bootstrap CSS 文件和 Bootstrap JS 文件(Bundle)Only,它应该可以工作,我还要注意 Bootstrap 5 don't need jQuery to work.
  • 是的。我必须开始。我认为我的 bootstrap.js 文件有问题 - 我不知道它可能已经以某种方式损坏了。它现在可以工作(使用您提到的设置)。很高兴了解 jquery,但我想我会保留它 - 使用它可能比使用干净的 javascript 更容易。
  • 凯文的评论应被视为答案。为了在 bootstrap 5 的情况下进行澄清,您必须删除 popper 和 jQuery 并使用 bootstrap.bundle.js。

标签: javascript bootstrap-5


【解决方案1】:

每次应该显示弹出框时,我都会收到此错误 (TypeError: i.createPopper is not a function)。以前我不得不使用单独的 Popper 和 Bootstrap JS(选项 2) - 由于与其他脚本冲突。然而,这在我的项目中不再需要,使用 Bootstrap 5.1.1。

仅包括带有 popper 的 Bootstrap 捆绑包 - 根据上面@Kevin 的评论 - 为我解决了这个问题 - 以前的冲突似乎不再是问题了。所有弹出框、工具提示、模式、侧边栏等现在似乎都可以正常工作:

<body>
    <!-- Option 1: Bootstrap Bundle with Popper -->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>    
</body> 

【讨论】:

  • 我现在面临同样的问题,正如@Daniel 上面所说,我使用 bootstrap.bundle.min.js 而不是 Popper.js 和 Bootstrap.min.js 文件解决了我的问题。希望这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 2022-11-19
  • 1970-01-01
  • 2019-06-06
相关资源
最近更新 更多