简体   繁体   English

angular 7 CLI应用程序中的jQuery日期选择器不起作用

[英]JQuery datepicker in angular 7 cli app not working

I am trying to get the jquery datepicker to work in the application that I'm building. 我正在尝试使jquery datepicker在正在构建的应用程序中工作。 I have an angular cli app. 我有一个角度cli应用程序。 I've added jquery-datepicker with npm and jquery with npm. 我添加了带有npm的jquery-datepicker和带有npm的jquery。

package.json dependencies looks like: package.json依赖项如下所示:

"@angular/animations": "~7.1.0",
"@angular/common": "~7.1.0",
"@angular/compiler": "~7.1.0",
"@angular/core": "~7.1.0",
"@angular/forms": "~7.1.0",
"@angular/platform-browser": "~7.1.0",
"@angular/platform-browser-dynamic": "~7.1.0",
"@angular/router": "~7.1.0",
"@ng-bootstrap/ng-bootstrap": "^4.0.1",
"bootstrap": "^4.2.1",
"core-js": "^2.5.4",
"font-awesome": "^4.7.0",
"jquery": "^3.3.1",
"jquery-datepicker": "^1.12.3",
"moment": "^2.23.0",
"popper.js": "^1.14.6",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"web-animations-js": "^2.3.1",
"zone.js": "~0.8.26"

The error that I get is: 我得到的错误是:

ERROR TypeError: jquery__WEBPACK_IMPORTED_MODULE_4__(...).datepicker is not a function 错误类型错误:jquery__WEBPACK_IMPORTED_MODULE_4 __(...)。datepicker不是一个函数

pollyfills.ts looks like: pollyfills.ts看起来像:

import 'jquery';
import 'popper.js';
import 'bootstrap';
import 'moment';
import 'jquery-datepicker';

Then my component looks like: 然后我的组件看起来像:

ngOnInit() {
    $( "#datepicker" ).datepicker();
  }

Can someone tell me why I would be getting this error? 有人可以告诉我为什么会出现此错误吗?

  1. Create a jquery.ts in the same location as your main.ts and paste the following in it, 在与main.ts相同的位置创建一个jquery.ts并将以下内容粘贴到其中,

    import * as $ from 'jquery'; 从'jquery'导入*作为$;

    window['jQuery'] = window['$'] = $; window ['jQuery'] = window ['$'] = $;

  2. Insert the following at the top of your main.ts, 在main.ts的顶部插入以下内容,

    import 'jquery'; 导入'jquery';

    import 'popper.js'; 导入'popper.js';

    import 'bootstrap'; 导入'bootstrap';

    import 'moment'; 导入“时刻”;

    import 'jquery-datepicker'; 导入'jquery-datepicker';

  3. Remove any other script references to jquery or bootstrap from your html or angular cli config. 从您的html或angular cli配置中删除对jquery或bootstrap的所有其他脚本引用。

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

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