我正在使用 jquery 测试 typescript,但是当我编译 test.ts 文件时,它总是给我一个错误,指示:找不到名称“$”。
我已经导入了 jquery 并添加了它的定义参考。
如果我使用import $ = require("jquery")
in my test.ts
文件,另一个错误”Cannot find module jquery
“执行时会发生tsc
编译。但是,JQuery 文件夹已存在于node_modules 文件夹中。
有谁知道在打字稿中使用 jquery 的正确方法是什么?
下面是我的步骤:
- 使用安装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");
如果你发现这些错误 90% 的情况是因为 @types/jquery 的版本控制问题
尝试运行:
npm install jquery --save
Then in app.module.ts
:
import * as $ from 'jquery';
然后运行:
npm install @types/[email protected] /cdn-cgi/l/email-protection
你应该准备好出发了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)