简体   繁体   English

Bootstrap 4 错误“Bootstrap dropdown require Popper.js”,使用 Aurelia CLI 和 Require.js

[英]Bootstrap 4 error “Bootstrap dropdown require Popper.js”, with Aurelia CLI and Require.js

I'm having trouble configuring Bootstrap 4 beta in an Aurelia CLI app (v0.31.1) with requirejs and using TypeScript.我在使用 requirejs 和 TypeScript 的 Aurelia CLI 应用程序 (v0.31.1) 中配置 Bootstrap 4 beta 时遇到问题。 After having tried several config variations I keep on getting the following console error:在尝试了几种配置变体后,我不断收到以下控制台错误:

Uncaught Error: Bootstrap dropdown require Popper.js未捕获的错误:Bootstrap 下拉菜单需要 Popper.js

Here are the steps to reproduce.以下是重现的步骤。 First, install the packages:首先,安装软件包:

$ npm install --save jquery bootstrap@4.0.0-beta popper.js

Next, I've configured aurelia.json :接下来,我配置了aurelia.json

  "jquery",
  {
    "name": "popper.js",
    "path": "../node_modules/popper.js/dist/umd",
    "main": "popper"
  },
  {
    "name": "bootstrap",
    "path": "../node_modules/bootstrap/dist",
    "main": "js/bootstrap.min",
    "deps": [
      "jquery",
      "popper.js"
    ],
    "exports": "$",
    "resources": [
      "css/bootstrap.css"
    ]
  }

Notice in the config above that:注意上面的配置:

  • popper.js is registered before bootstrap popper.js 在引导前注册
  • the UMD module is used使用 UMD 模块
  • popper.js is set as a dependency for bootstrap, next to jquery popper.js 设置为 bootstrap 的依赖项,在 jquery 旁边

Finally, in my app.ts :最后,在我的app.ts 中

import 'bootstrap';

With this configuration, building using au build works fine.使用此配置,使用au build工作正常。 But upon running, using au run --watch I get the following console errors:但是在运行时,使用au run --watch我收到以下控制台错误:

Uncaught Error: Bootstrap dropdown require Popper.js ( https://popper.js.org ) (defaults.js:19)未捕获的错误:Bootstrap 下拉菜单需要 Popper.js ( https://popper.js.org ) (defaults.js:19)
Uncaught Error: Bootstrap dropdown require Popper.js ( https://popper.js.org ) (bootstrap.min.js:6)未捕获的错误:Bootstrap 下拉菜单需要 Popper.js ( https://popper.js.org ) (bootstrap.min.js:6)
... a bit further on: ……再进一步:
Uncaught TypeError: plugin.load is not a function at Module.未捕获的类型错误:plugin.load 不是模块中的函数。 (defaults.js:19) (defaults.js:19)

Unfortunately, the Bootstrap 4 docs only mention instructions on webpack .不幸的是,Bootstrap 4 文档只提到了关于 webpack 的说明 So does a search on both Aurelia's Gitter.im channel and on StackOverflow.在 Aurelia 的Gitter.im 频道和 StackOverflow 上进行搜索也是如此 I cannot find samples regarding Aurelia CLI with Require.js.我找不到有关带有 Require.js 的 Aurelia CLI 的示例。 Finally, Google hits shows only examples for embedding the alpha versions (which relied on 'tethering' rather than 'popper').最后,Google hits 仅显示了嵌入 alpha 版本的示例(依赖于“tethering”而不是“popper”)。

Similar questions on SO, which have the same error but aren't applicable to my situation: SO上的类似问题,具有相同的错误,但不适用于我的情况:

So, my question: how can I configure Bootstrap 4 with Popper.js in an Aurelia CLI app (using Require.js, not Webpack)?所以,我的问题是:如何在 Aurelia CLI 应用程序中使用 Popper.js 配置 Bootstrap 4(使用 Require.js,而不是 Webpack)?

Thanks.谢谢。

Popper replaced Tether in the beta.波普尔在测试版中取代了 Tether。

As such you will need to swap out tether with popper (or just add it if you never had the alpha) to the prepend section of your aurelia.json file.因此,您需要将 tether 与 popper 交换(或者,如果您从未使用过 alpha,则只需添加它)到您的 aurelia.json 文件的 prepend 部分。 (Make sure you link to the UMD version seen below) (确保您链接到下面看到的 UMD 版本)

"prepend": [
   ...
          "node_modules/jquery/dist/jquery.js",
          "node_modules/popper.js/dist/umd/popper.min.js",
   ...
     ]

You will also need the bundling as expected, something like this:您还需要按预期进行捆绑,如下所示:

      {
        "name": "bootstrap4",
        "path": "../node_modules/bootstrap/dist",
        "main": "js/bootstrap.min",
        "deps": [
          "jquery"
        ],
        "exports": "$",
        "resources": [
          "css/bootstrap.css"
        ]
      }

=Addendum= =附录=

Unlike tether, popper does not need to be prepended it seems.与 Tether 不同,popper 似乎不需要预先添加。 So you could include it like any other dependency with所以你可以像任何其他依赖一样包含它

 {
     "name": "popper.js",
     "path": "../node_modules/popper.js/dist/umd",
     "main": "popper.min"
 },

I uninstalled popper.js and used the version built into bs4 by using js/bootstrap.bundle.min instead of js/bootstrap.min我卸载了 popper.js 并通过使用js/bootstrap.bundle.min而不是js/bootstrap.min使用了内置到 bs4 中的版本

  "jquery",
  {
    "name": "bootstrap",
    "path": "../node_modules/bootstrap/dist",
    "main": "js/bootstrap.bundle.min",
    "deps": ["jquery"],
    "exports": "$",
    "resources": [
      "css/bootstrap.css"
    ]
  },

Add in your code.添加您的代码。

<!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

Using the bundled version of BS4 worked for me as follows :-使用捆绑版本的 BS4 对我有用,如下所示:-

Remove popper npm uninstall popper.js移除 popper npm uninstall popper.js

Update to BS4 or later npm install bootstrap --save更新到 BS4 或更高版本npm install bootstrap --save

Make sure jquery is installed npm install bootstrap --save确保安装了 jquery npm install bootstrap --save

Edit `.angular-cli.json' to include jquery and bundled BS4编辑 `.angular-cli.json' 以包含 jquery 和捆绑的 BS4

   "scripts": [
    "../node_modules/jquery/dist/jquery.slim.min.js",
    "../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
   ],

as of final bootstrap 4.0 below is the working conf for bootstrap with popper从下面的最终引导程序 4.0 开始,是带有 popper 的引导程序的工作配置

//file; aurelia_project/aurelia.json
"dependencies": [   
    ...
    ... // other dependencies
    ...

   "text",
   "jquery",
   {
     "name": "popper.js",
     "path": "../node_modules/popper.js/dist/umd",
     "main": "popper.min"
   },
   {
     "name": "bootstrap",
     "path": "../node_modules/bootstrap/dist",
     "main": "js/bootstrap.min",
     "deps": ["jquery"],
     "exports": "$",
     "resources": [
       "css/bootstrap.css"
     ]
   },
...
... // remaining dependencies
...
],

This works with latest aurelia & bootstrap (as of 2018-Feb) without using the prepend method这适用于最新的 aurelia 和 bootstrap(截至 2018 年 2 月),而无需使用 prepend 方法

With the new bootstrap 4 you can just include this:使用新的引导程序 4,您可以只包含以下内容:

<script type="text/javascript" src="/assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js">
</script>

Popper.js is already included in this file. Popper.js 已包含在此文件中。 Check bootstrap documentation dropdowns: https://getbootstrap.com/docs/4.0/components/dropdowns/#overview检查引导程序文档下拉列表: https : //getbootstrap.com/docs/4.0/components/dropdowns/#overview

PS: You need to install bootstrap 4 ofcourse! PS:当然需要安装bootstrap 4! It might not be exactly your answer but it might help alot for other people who aren't using Aurelia CLI and Require.js这可能不完全是您的答案,但它可能对不使用 Aurelia CLI 和 Require.js 的其他人有很大帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM