打字稿和 JQuery 编译错误:找不到名称“$”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38283236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript and JQuery compile error: Cannot find name '$'
提问by Mark Yuan
I'm testing typescript with jquery, but when I compile the test.ts file, it always gives me an error indicating: Cannot find name '$'.
我正在用 jquery 测试打字稿,但是当我编译 test.ts 文件时,它总是给我一个错误指示:找不到名称“$”。
I've already imported jquery & added its definition reference.
If I use import $ = require("jquery")
in my test.ts
file, another error "Cannot find module jquery
" will occur when doing the tsc
compiling. However, the JQuery folder already exists within the node_modules folder.
我已经导入了 jquery 并添加了它的定义参考。如果我import $ = require("jquery")
在我的test.ts
文件中使用,Cannot find module jquery
则在tsc
编译时会出现另一个错误“ ” 。但是,JQuery 文件夹已存在于 node_modules 文件夹中。
Does anyone know what is the correct way to use jquery in typescript?
有谁知道在打字稿中使用 jquery 的正确方法是什么?
Below is my steps:
下面是我的步骤:
- Install jquery using
npm install jquery --save
- Install typings & jquery definition using
typings install --global --save dt~jquery
- Add jquery reference at top of test.ts
/// <reference path="../../../typings/globals/jquery/index.d.ts" />
- 安装 jquery 使用
npm install jquery --save
- 使用安装类型和 jquery 定义
typings install --global --save dt~jquery
- 在 test.ts 顶部添加 jquery 引用
/// <reference path="../../../typings/globals/jquery/index.d.ts" />
tsconfig.json
配置文件
{
"compilerOptions": {
"jsx": "react",
"outDir": "./dist",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"experimentalDecorators": true
},
"exclude": [
"node_modules"
],
"files": [
"./typings/index.d.ts",
"./src/wo/tests/test.ts",
]
}
test.ts
测试文件
/// <reference path="../../../typings/globals/jquery/index.d.ts" />
let test:any=$("div");
回答by Ignatius Andrew
If you find these errors 90% of the time its because of versioning Problem of @types/jquery
如果你发现这些错误 90% 是因为 @types/jquery 的版本控制问题
Try running:
尝试运行:
npm install jquery --save
Then in app.module.ts
:
然后在app.module.ts
:
import * as $ from 'jquery';
Then run:
然后运行:
npm install @types/[email protected]
And you should be ready to go.
你应该准备好了。
回答by Nahid Shaiket
I don't know if it helps but I solved the same issue for my datepicker.
我不知道它是否有帮助,但我为我的日期选择器解决了同样的问题。
First I installed jQuery with this command: npm install --save-dev @types/jquery
首先,我使用以下命令安装了 jQuery: npm install --save-dev @types/jquery
Then add the dependencies in your angular-cli.json file:
然后在 angular-cli.json 文件中添加依赖项:
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"../node_modules/font-awesome/css/font-awesome.css",
"../node_modules/ng2-datetime/src/vendor/bootstrap-datepicker/bootstrap-datepicker3.min.css",
"styles.css"
]
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"../node_modules/ng2-datetime/src/vendor/bootstrap-datepicker/bootstrap-datepicker.min.js"
]
I think adding the jQuery part might work out for you. Please let me know if it works.
我认为添加 jQuery 部分可能适合您。请让我知道它是否有效。
回答by ChillyWilly
import * as $ from "jquery";
didn't work for me. Interesting enough,
对我不起作用。够有趣,
import { ajax, css } from "jquery";
actually solved my problem.
实际上解决了我的问题。
回答by Ezhil Arasan
Install and import following package. it will solved for error ("Cannot find name '$'"). Angular version is 7
安装并导入以下包。它将解决错误(“找不到名称'$'”)。Angular 版本是 7
import 'datatables.net';
导入'datatables.net';
回答by T.Dossev
You probably have some vulnerabilities you need to fix.
您可能有一些需要修复的漏洞。
Run npm audit fix
to fix them, or npm audit
for details.
运行npm audit fix
以修复它们,或npm audit
了解详细信息。