问题描述
我正在用jquery测试打字稿,但是当我编译test.ts文件时,它总是给我一个错误,指示:找不到名称"$".
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
文件中使用import $ = require("jquery")
,则在执行tsc
编译时将出现另一个错误"Cannot find module jquery
".但是,node_modules文件夹中已经存在JQuery文件夹.
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的正确方法是什么吗?
Does anyone know what is the correct way to use jquery in typescript?
以下是我的步骤:
- 使用
npm install jquery --save
安装jquery - 安装键入&使用
typings install --global --save dt~jquery
定义jquery - 在test.ts
/// <reference path="../../../typings/globals/jquery/index.d.ts" />
顶部添加jquery引用
- 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" />
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");
推荐答案
如果90%的时间发现这些错误,则是由于版本控制引起的@ types/jquery问题
If you find these errors 90% of the time its because of versioning Problem of @types/jquery
尝试运行:
npm install jquery --save
然后在app.module.ts
中:
import * as $ from 'jquery';
然后运行:
npm install @types/[email protected]
您应该准备出发了.
这篇关于Typescript和JQuery编译错误:找不到名称"$"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!