Can't Find JQuery When Running 'ng test' Command

主宰稳场 提交于 2019-12-12 22:39:23

问题


I have created a project using new angular-cli version and I made the project up and running. When I run ng serve and ng build command it works perfectly without giving any errors. But when I tried to run ng test it shows me errors like this.

theme.provider.ts (53,31): Property 'removeClass' does not exist on type 'JQuery'.
theme.provider.ts (64,35): Property 'addClass' does not exist on type 'JQuery'.
theme.provider.spec.ts (7,30): Cannot find name '$'.

I have added JQuery in the following way to the angular-cli.json

"scripts": [
    "../node_modules/jquery/dist/jquery.min.js"
 ],

Then I have imported JQuery to my main module via this way

import 'jquery';

Are there any missing steps in the above configurations?


回答1:


Assuming you have @types/jquery installed, try adding jquery to the list of types in tsconfig.spec.json (it should already have jasmine and node in the list).


Also, when you add jquery.min.js to the scripts list in angular-cli.json, it ends up in your scripts.bundle.js. When you then do import 'jquery'; in your main module it also ends up in your vendor.bundle.js, so you have added the code twice.

Instead of doing import 'jquery';, just add jquery to your compilerOptions types list in tsconfig.app.json (or create one "types": ["jquery"]). You will then be able to reference the global jquery object without importing it a second time.



来源:https://stackoverflow.com/questions/46701835/cant-find-jquery-when-running-ng-test-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!