javascript - 无法将外部js与Angular2一起使用

标签 javascript angular typescript

我正在尝试导入 twitter-bootstrap-wizard我将 javascript 库添加到我的 angular 2 项目中,但总是收到此错误:

WEBPACK_IMPORTED_MODULE_1_jquery(...).bootstrapWizard is not a function

我只是使用 angular-cli 1.0.0 创建一个新的 angualr 应用程序,然后在 index.html 中添加脚本,如下所示:

  <body>
     <app-root>Loading...</app-root>
  <script  src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap-wizard/1.2/jquery.bootstrap.wizard.js"></script>
  </body>

然后在我的组件中:

//
declare var bootstrapWizard : any;
import * as $ from 'jquery';
//

 ngAfterViewInit() {
    (<any>$('#wizard')).bootstrapWizard()
 }

最佳答案

如果你使用cdn,只需添加声明

declare const jQuery: any;

或者您可以删除 cdn,然后从 npm 安装 jquery,然后使用您的导入

npm i --save jquery

import * as $ from 'jquery';

关于javascript - 无法将外部js与Angular2一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452155/

相关文章:

javascript - jQuery .each 函数输出之前每个循环的值

内部带有 HTML 标记的 Angular 2 i18n 消息

html - 如何使用 Typescript 在 React 中扩展 HTML 属性

javascript - JavaScript 文件顶部的 "format register"字符串是什么?

javascript - 无法读取未定义(…)的属性 '_aData' - 数据表

Javascript,CSS 在 Google Chrome Android 中不起作用

angular - Ionic 3 "ionic cordova build android --prod"错误元数据版本不匹配

javascript - 将多个模块包装在单个模块 Angular 2 中?

javascript - 将鼠标事件冒泡到 Chart.js 的父 Canvas

javascript - 无法安装 jest