【问题标题】:Add jquery to angular 2 wepack project (Bootstrap's JavaScript requires jQuery)将 jquery 添加到 angular 2 wepack 项目(Bootstrap 的 JavaScript 需要 jQuery)
【发布时间】:2017-08-15 14:27:06
【问题描述】:

我正在使用 angular-cli .net 核心启动器。现在我正在添加自己的 css + javascript 并收到以下错误:

处理请求时发生未处理的异常。 异常:调用 Node 模块失败并出现错误:预渲染因错误而失败:错误:Bootstrap 的 JavaScript 需要 jQuery

我已经试过了:

npm install jquery 

+

import $ from 'jquery'; => in app component

如何使用 webpack / angular2 配置 jquery?

谢谢!

【问题讨论】:

  • 为什么不使用完全不需要 jQuery 和 Bootstrap 的 JS 的专用 Bootstrap + Angular 集成? ng-bootstrap.github.io

标签: jquery css twitter-bootstrap angular webpack


【解决方案1】:

安装类型定义:

npm install @types/jquery

【讨论】:

    【解决方案2】:

    步骤

    1. npm install jquery — save

    2. "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ] 添加到 angular-cli.json 文件中

    3. 将其导入组件:import * as $ from 'jquery';

    4. 例子:

      $(document).ready(function(){
          $("button").click(function(){ 
           ...
         } 
      });
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 2017-11-19
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2017-06-06
      相关资源
      最近更新 更多