【问题标题】:Getting exception jQuery must be included before Bootstrap's JavaScript获取异常 jQuery 必须在 Bootstrap 的 JavaScript 之前包含
【发布时间】:2020-02-18 11:01:17
【问题描述】:

我遇到了异常

bootstrap.js:240 Uncaught TypeError: Bootstrap 的 JavaScript 需要 jQuery。 jQuery 必须在 Bootstrap 的 JavaScript 之前包含

在 Object.jQueryDetection (bootstrap.js:240) 在 bootstrap.js:255 在 bootstrap.js:9 在 bootstrap.js:10

这是我在Angular.json中的代码

"styles": [
  "src/styles.css",
  "node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts": 
[
  "node_modules/jquery/dist/jquery.js",
  "node_modules/bootstrap/dist/js/bootstrap.js"           
]

我该如何解决这个问题?

【问题讨论】:

    标签: jquery angular twitter-bootstrap


    【解决方案1】:

    "node_modules/jquery/dist/jquery.min.js" 肯定应该在“脚本”下吗?

    "styles": [
      "src/styles.css",
      "node_modules/bootstrap/dist/css/bootstrap.css"
    ],
    "scripts": 
    [
      "node_modules/jquery/dist/jquery.min.js",
      "node_modules/bootstrap/dist/js/bootstrap.js"           
    ]
    

    现在重复的 jQuery 文件 ""node_modules/jquery/dist/jquery.js" 应该被删除。

    【讨论】:

    • 我仍然遇到同样的错误。我已经用我的代码替换了你的代码。
    • 您是否检查过您的文件系统以查看这些文件路径是否存在?
    • 在添加这些脚本之前,我确保已将 jQuery 和 Bootstrap 正确安装到项目中。如果未安装 jQuery,您将收到错误消息。库尔特给出的答案 100% 正确。
    【解决方案2】:

    如果您想在您的应用程序中使用引导程序,或者如果您已经在您的项目中使用,请确保在包含引导程序 JavaScript 文件之前包含 jQuery。 Bootstrap 的 JavaScript 文件需要 jQuery。

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

    包含 jQuery 后,停止运行 Angular CLI 应用程序。然后,使用ng serve 重新运行它。

    然后,如果你想在代码中使用 jQuery,你所要做的就是将它导入到任何你想使用 jQuery 的组件中。

    import * as $ from 'jquery';
    

    declare var $: any;
    

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 2018-03-17
      • 2020-11-15
      • 2019-10-29
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多