ng test fail after adding 'ng2-dateime '

65 Views Asked by At

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

4

There are 4 best solutions below

0
Manuel Rodriguez On

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;
1
Boulboulouboule On

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

0
AudioBubble On

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

0
Nitish On

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

After this it started working.