【问题标题】:Bootstrap: Uncaught TypeError: Cannot read property 'fn' of undefined引导程序:未捕获的类型错误:无法读取未定义的属性“fn”
【发布时间】:2018-12-31 21:08:07
【问题描述】:

我正在尝试使用 Bootstrap 制作一个 Electron 应用程序。我收到此错误消息:

Uncaught TypeError: Cannot read property 'fn' of undefined
at setTransitionEndSupport (bootstrap.js:122)
at bootstrap.js:199
at bootstrap.js:201
at bootstrap.js:9
at bootstrap.js:10

我在 package.json 中的依赖是:

"dependencies": {
  "bootstrap": "^4.1.2",
  "electron": "^2.0.5",
  "jquery": "^3.3.1",
  "popper.js": "^1.14.3"
}

我的 index.html 文件是:

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Release Management Report</title>

</head>

<body>

  <div class="container">

    <h1>Bootstrap Test</h1>

    <p>
        We are using node
        <script>document.write(process.versions.node)</script>, Chrome
        <script>document.write(process.versions.chrome)</script>, and Electron
        <script>document.write(process.versions.electron)</script>.
    </p>

  </div>

  <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>

</body>

</html>

然后由E​​lectron快速启动的main.js加载html文件:

const { app, BrowserWindow } = require('electron')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({ width: 800, height: 600 })

  // and load the index.html of the app.
  win.loadFile('index.html')

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})

我发现的大多数解决方案都告诉我在 Bootstrap 之前导入 jQuery,但这已经是我正在做的事情了。

感谢您的帮助!

【问题讨论】:

  • 您能告诉我们您的 html 文件是如何加载的吗?
  • @Thaadikkaaran 是的,我使用了电子快速入门中的 main.js,我已经在问题中添加了它
  • 我有同样的问题,使用完全相同的依赖项和版本,但没有使用电子。我正在从 Rails 模板加载。
  • @nuno 你好,我找到了一个适合我的解决方案。我将其添加为答案。也许它也对你有用。
  • 嗨!格里福德!该问题似乎与库加载顺序有关。我没有使用与使用 Rails 资产管道完全相同的解决方案,但也设法修复了它。基本上 jquery 必须在 bootstrap 之前加载,我以为我在做但实际上不是。谢谢分享!

标签: jquery node.js bootstrap-4 electron


【解决方案1】:

我找到了解决问题的方法。

我在 index.html 中添加了一个脚本:

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

这是 script.js 的内容:

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

我们可以从 index.html 中删除这些行以避免重复导入:

<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>

我在https://github.com/understrap/understrap/issues/449找到了这个解决方案, 根据 karo1915 的评论。

【讨论】:

  • 我相信我们必须先在index.html中加载require JS
  • 我收到 Uncaught ReferenceError: require is not defined
【解决方案2】:

感谢 Griford 的回答,我设法让我的 Angular&Electron 应用程序使用 Bootstrap 运行。我想在这里贡献的只是不需要外部文件 - 您也可以在脚本块中初始化 JQuery 和 Bootstrap:

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

这适用于以下版本(Electron 2.0.7):

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

注意:当上面两行初始化时,使用 Bootstrap 不需要其他脚本集成。您唯一需要的就是 CSS(在我的例子中是通过 angular.json 添加到包中的)。

【讨论】:

    【解决方案3】:

    就我而言,我的脚本导入顺序错误。 bootstrap 导入应该在 popper 和 jquery 导入之后。

    由于 Bootstrap v4.+ 依赖于 popper.js 和 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"
            ]
    

    【讨论】:

      【解决方案4】:

      我在 bootstrap 4.1.3 中遇到同样的错误,我确信脚本顺序是正确的,jquery 放在 bootstrap 之前。

      因为我通过@require 属性将这些脚本加载到tampermonkey 脚本(用于管理可以修改当前网页的第三方脚本的浏览器插件)中,所以很难在jquery 加载和引导加载之间注入代码。

      更改为引导程序的先前版本3.3.7 解决了错误。等待下一个版本的引导程序会解决这个问题。

      【讨论】:

        猜你喜欢
        • 2014-01-03
        • 1970-01-01
        • 2021-12-22
        • 2015-01-06
        • 2017-07-26
        • 2013-07-23
        • 2019-02-26
        相关资源
        最近更新 更多