简体   繁体   English

使用 require.js 加载时,在 bootstrap 4 中加载 popper.js 时出错

[英]Error load popper.js in bootstrap 4 when use require.js to load

I installed bootstrap 4, jquery, popper by npm.我通过 npm 安装了 bootstrap 4、jquery、popper。 When I load website it show error in console about can't get file popper.js, I check again that I see one file popper.js was load by require.js and one file popper.js was noticed was not found by bootstrap.当我加载网站时,它在控制台中显示关于无法获取文件 popper.js 的错误,我再次检查我是否看到一个文件 popper.js 是由 require.js 加载的,而另一个文件 popper.js 被注意到没有被引导程序找到。 They have different path.他们有不同的路径。 file success have path: http://localhost:9000/Libs/popper.js/dist/umd/popper.js and file fail is http://localhost:9000/popper.js文件成功有路径: http://localhost:9000/Libs/popper.js/dist/umd/popper.js文件失败是http://localhost:9000/popper.js

In below picture, pic 1 show configure path of library js.在下图中,图 1 显示库 js 的配置路径。 Pic 2 show librarys was load success, and pic 3 notice popper.js was not load.图 2 显示库加载成功,图 3 通知 popper.js 未加载。

I don't understand why bootstrap can't recognize popper.js was loaded by requie.js.我不明白为什么 bootstrap 无法识别 popper.js 是由 requie.js 加载的。

Everyone can help me explain about problem for me.大家可以帮我解释一下问题。 Thanks!谢谢!

在此处输入图片说明

Try using:尝试使用:

bootstrap.bundle.js

or:或:

bootstrap.bundle.min.js

https://getbootstrap.com/docs/4.0/getting-started/contents/ https://getbootstrap.com/docs/4.0/getting-started/contents/

The docs show that those two files mentioned come with popper included so you won't need to require it.文档显示提到的这两个文件包含 popper,因此您不需要它。

If that doesn't work search your source files for /popper.js it may be as simple as fixing a reference somewhere else.如果这不起作用,请搜索 /popper.js 的源文件,这可能就像在其他地方修复引用一样简单。

Instead of doing this:而不是这样做:

require(["bootstrap"], function(bootstrap) {
    // do nothing
});

Try this which loads popper first:试试这个先加载 popper:

require(["popper"], function(popper) {
    window.Popper = pop
    require(["bootstrap"]);
});

Alternatively you can use the CDN links.或者,您可以使用 CDN 链接。 Bootstrap suggests using the CDN versions before you include the compiled bootstrap. Bootstrap 建议在包含编译的引导程序之前使用 CDN 版本。

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

Hey this is my first response on a stackoverflow question but since I have been struggling with it myself, I wanted to share my answer.嘿,这是我对 stackoverflow 问题的第一个回答,但由于我自己一直在努力解决这个问题,所以我想分享我的答案。 Just like other answers say, bootstrap requires the popper js but it references popper itself on the root so it doesn't matter if you load it or not, bootstrap looks for it on the root while you have it on a deeper directory.就像其他答案所说的那样,bootstrap 需要 popper js,但它在根目录上引用 popper 本身,因此无论您是否加载它都没有关系,bootstrap 在根目录上查找它,而您将它放在更深的目录中。 Look at the code below and you can see it uses require('popper.js'):看看下面的代码,你可以看到它使用了 require('popper.js'):

bootstrap.js引导程序.js

The solution is to use require to define module "popper.js" and exactly as this name like below:解决方案是使用 require 来定义模块“popper.js”,就像下面这个名字一样:

require.config({
baseUrl: 'lib',
paths: {
    main: 'main',
    jquery: 'jquery/dist/jquery',
    'jquery-slim': 'jquery/dist/jquery-3.3.1.slim.min',
    'jqueryUnobtrusive': 'jquery-validation-unobtrusive/jquery.validate.unobtrusive',
    'bootstrap': 'bootstrap/dist/js/bootstrap',
    'knockout': 'knockout/knockout-latest',
    'ajax': 'jquery-ajax/jquery.unobtrusive-ajax',
    'shop': '/scripts/shop/shop',
    'domready': 'requirejs/domready',
    'shoporder': '/scripts/shoporder/shoporder',
    'popper': 'popper.js/dist/umd/popper'
}});

define("popper.js", ['popper'], () => {
require(['popper'], () => {
    require(['bootstrap']);
});

define(['main', 'jquery', 'jquery-slim'], () => {
 require(['jqueryUnobtrusive', 'ajax']);
 require(['knockout']);
 require(['jquery']);
 require(['popper.js']);
});

This way bootstrap looks for the correct directory.这样引导程序会查找正确的目录。

Bootstrap requires Popper to be available in the global scope, you can solve the issue by adding exports to the shim configuration: Bootstrap 要求 Popper 在全局范围内可用,您可以通过将导出添加到 shim 配置来解决该问题:

shim: {
    bootstrap: {
        deps: ['jquery','popper']
    },
    popper: {
        exports: "Popper"
    }
}

Also you need to make sure that bootstrap defines the name of the module correctly.您还需要确保引导程序正确定义了模块的名称。 You can check that in the opening lines the bootstrap.js file您可以在开始行中检查 bootstrap.js 文件

(function (global, factory) {   
  typeof exports === 'object' && typeof module !== 'undefined' 
  ?  factory(exports, require('jquery'), require('popper')) 
  :  typeof define === 'function' && define.amd 
  ? define(['exports', 'jquery', **'popper'**], factory) 
  : (...)

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

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