运行'ng test'命令时找不到JQuery [英] Can't Find JQuery When Running 'ng test' Command
问题描述
我使用新的 angular-cli 版本创建了一个项目,并启动并运行了该项目.当我运行 ng serve 和 ng build 命令时,它可以完美运行而不会出现任何错误.但是当我尝试运行 ng test 时,它向我显示了这样的错误.
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 '$'.
我已通过以下方式将 JQuery 添加到 angular-cli.json
I have added JQuery in the following way to the angular-cli.json
"scripts": [
"../node_modules/jquery/dist/jquery.min.js"
],
然后我通过这种方式将 JQuery 导入到我的主模块中
Then I have imported JQuery to my main module via this way
import 'jquery';
以上配置是否有遗漏的步骤?
Are there any missing steps in the above configurations?
推荐答案
假设您安装了 @types/jquery
,请尝试将 jquery
添加到 jquery
中的类型列表中code>tsconfig.spec.json(列表中应该已经有 jasmine
和 node
).
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).
此外,当您将 jquery.min.js
添加到 angular-cli.json
中的 scripts
列表时,它最终会出现在您的scripts.bundle.js
.当您在主模块中执行 import 'jquery';
时,它也 最终会出现在您的 vendor.bundle.js
中,因此您已添加代码两次.
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.
不要执行 import 'jquery';
,只需将 jquery
添加到 tsconfig.app.json 中的
(或创建一个 compilerOptions
类型列表中"types": ["jquery"]
).然后,您将能够引用全局 jquery 对象,而无需再次导入它.
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.
这篇关于运行'ng test'命令时找不到JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!