0

I have added ng2-datetime module in my Angular 4 project.

Now problem I am facing with ng test.

Currently it throws error like below.

'Cannot find name 'jQuery'
'Cannot find name '$'

EDIT:

I did tried same way as most of answer below which is adding below lines in typings.d.ts

declare var jQuery: any;
declare var $: any;

this will start 'ng test' but it will throw error while making build using ng build --prod.

Cannot redeclare block-scoped variable 'jQuery'
Cannot redeclare block-scoped variable '$'

TIA

Nitish
  • 651
  • 1
  • 7
  • 14

4 Answers4

1

Try to add this on your typings.d.ts :

declare jQuery: any;

The variable will be available in all your app

But if you can, try to avoid plugins that uses jQuery

1

I didn't added 'jQuery' in 'types' section in tsconfig.spec.json file.

After this it started working.

Nitish
  • 651
  • 1
  • 7
  • 14
0

In index.html put <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>in a tag and declare this variables in the component.ts where you are using them

declare var jquery:any;
declare var $ :any;
0

This issue appens when you use a plugin that requires jQuery without declaring the jQuery variable. You can fix it by adding this on top of your component:

declare jQuery: any;

However, using jQuery to accessing your DOM isn't a good practice in Angular projects, and will be annoying when you'll want to implement things like angular universal on your project.

Try with the ngx-bootstrap plugin, which contains a cleaner datepicker :

https://valor-software.com/ngx-bootstrap/#/datepicker

Boulboulouboule
  • 4,087
  • 1
  • 13
  • 29
  • Your second error append because it seems you're trying to redeclare a variable that is already declared. Maybe `jQuery` is declared in your component AND in your `typings.d.ts`. Remove the component variable – Boulboulouboule Nov 14 '17 at 10:35