Bootstrap: Uncaught TypeError: Cannot read property 'fn' of undefined

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:49:21

I found a way to solve the problem.

I added a script in the index.html:

<script src="scripts/script.js"></script>

and this is the content of script.js:

window.$ = window.jQuery = require('jquery'); // not sure if you need this at all
window.Bootstrap = require('bootstrap');

and we can remove these lines from index.html to avoid double import:

<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.js"></script>

I found this solution at: https://github.com/understrap/understrap/issues/449, under the comment from karo1915.

Thanks to the answer from Griford I managed to get my Angular&Electron app running with Bootstrap. All I wanted to contribute here is that no external file is needed - you can initialize JQuery and Bootstrap in a script block also:

<script>
  window.$ = window.jQuery = require('jquery');
  window.Bootstrap = require('bootstrap');
</script>

This works for me with the following versions (Electron 2.0.7):

"bootstrap": "^4.1.3",
"jquery": "^3.3.1",
"popper.js": "^1.14.4"

NOTE: no other script integration is needed for using Bootstrap when initialized by the two lines above. The only thing you need in addition is the CSS (in my case added to the bundle via angular.json).

In my case, I had the ordering of the imports of scripts wrong. The bootstrap import should come after the popper and jquery imports.

Since Bootstrap v4.+ depends on popper.js and jquery

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

I get same error with bootstrap 4.1.3, and I am sure script order is correct, jquery is placed before bootstrap.

Because I load these scripts in tampermonkey scripts( a browser add-on to manage third party scripts which can modify current web page ) by the @require attribute, it's hard to inject codes between jquery loading and bootstrap loading.

Changing to a previous version 3.3.7 of bootstrap solves the error. Waiting for next version of bootstrap would solve this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!