简体   繁体   English

将jQuery插件添加到angular应用

[英]Adding jQuery plugin to angular app

Frameworks used 使用的框架

  1. Angular: 5.2.3 角度:5.2.3
  2. Foundation(Zurb): 6.2.4 基础(Zurb):6.2.4

Created an angular app using 使用创建了一个角度应用

> ng new foundation-plugin-app --style=sass
> cd foundation-plugin-app
> npm install foundation-sites --save

Everything work as expected till this. 在此之前,一切都按预期进行。
Then I added foundation-datepicker 然后我添加了foundation-datepicker

> npm install foundation-datepicker --save

This is how the plugin is initialized. 这是初始化插件的方式。

$('#scenario_start_date').fdatepicker({
  initialDate: '02-12-1989',
  format: 'mm-dd-yyyy',
  disableDblClickSelection: true,
  leftArrow: '<<',
  rightArrow: '>>',
  closeIcon: 'X',
  closeButton: true
});

Now, to make it work, followed these steps 1.Added styleUrls in app.component.ts 现在,使其工作,请按照下列步骤操作:1.在app.component.ts添加了styleUrls

styleUrls: [
    // ...,
    '../../node_modules/foundation-datepicker/scss/foundation-datepicker.scss'    ]

2.Added scripts in angular-cli.json 2,在angular-cli.json添加scripts

"scripts": [
    "node_modules/foundation-datepicker/js/foundation-datepicker.js"
]

3.Added typings to app.component.ts 3.Added typings ,以app.component.ts

npm install --save-dev @types/jquery

and imported jquery and foundation-datepicker plugin to the page 并将jqueryfoundation-datepicker插件导入页面

import * as $ from 'jquery';
import 'foundation-datepicker';

4.Lastly, called the plugin in ngOnInit() of app.component.ts 4.Lastly,称为在插件ngOnInit()app.component.ts

$('#scenario_start_date').fdatepicker(/*...*/);

Which gave error as 其中给错误为

fdatepicker is not defined. fdatepicker So I type-casted it as 所以我把它当成

(<any>$('#scenario_start_date')).fdatepicker(/*...*/);

to suppress the error. 抑制错误。

This gives a new error 这给出了一个新的错误

Uncaught TypeError: Cannot read property 'fn' of undefined 未捕获的TypeError:无法读取未定义的属性'fn'

at eval (webpack-internal:///../../../../foundation-datepicker/js/foundation-datepicker.min.js:1) 在评估时(webpack-internal:///../../../../foundation-datepicker/js/foundation-datepicker.min.js:1)
at eval (webpack-internal:///../../../../foundation-datepicker/js/foundation-datepicker.min.js:1) 在评估时(webpack-internal:///../../../../foundation-datepicker/js/foundation-datepicker.min.js:1)
at Object.../../../../foundation-datepicker/js/foundation-datepicker.min.js (vendor.bundle.js:36) 在对象... / .. / .. / .. / foundation-datepicker / js / foundation-datepicker.min.js(vendor.bundle.js:36)
at __ webpack_require __ (inline.bundle.js:55) 在__ webpack_require __(inline.bundle.js:55)
at eval (webpack-internal:///../../../../../src/app/app.component.ts:15) 在评估时(webpack-internal:///../../../../../src/app/app.component.ts:15)
at Object.../../../../../src/app/app.component.ts (main.bundle.js:21) 在对象... / .. / .. / .. / .. / src / app / app.component.ts(main.bundle.js:21)
at __ webpack_require __ (inline.bundle.js:55) 在__ webpack_require __(inline.bundle.js:55)
at eval (webpack-internal:///../../../../../src/app/app.module.ts:11) 评估时(webpack-internal:///../../../../../src/app/app.module.ts:11)
at Object.../../../../../src/app/app.module.ts (main.bundle.js:29) 在对象... / .. / .. / .. / .. / src / app / app.module.ts(main.bundle.js:29)
at __ webpack_require __ (inline.bundle.js:55) 在__ webpack_require __(inline.bundle.js:55)

What am I doing wrong with the code? 我的代码在做什么错? Any pointers will be helpful. 任何指针都会有所帮助。

Your import statement seems to be wrong. 您的导入声明似乎是错误的。

Please try import $ from 'jquery'; 请尝试import $ from 'jquery';

And make sure that jQuery is bundled with your generated bundle file. 并确保jQuery与您生成的捆绑文件捆绑在一起。

In your case it is either not globally exported or not included in the window context. 在您的情况下,它不是全局导出的,或者不包含在窗口上下文中。

In this case try 在这种情况下,请尝试

window.jQuery = $;
window.$ = $;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM