【发布时间】:2016-11-12 00:01:51
【问题描述】:
我正在用 jquery 测试 typescript,但是当我编译 test.ts 文件时,它总是给我一个错误提示:找不到名称'$'。
我已经导入了 jquery 并添加了它的定义参考。
如果我在我的test.ts 文件中使用import $ = require("jquery"),在进行tsc 编译时会出现另一个错误“Cannot find module jquery”。但是,JQuery 文件夹已经存在于 node_modules 文件夹中。
有谁知道在 typescript 中使用 jquery 的正确方法是什么?
以下是我的步骤:
- 使用
npm install jquery --save安装jquery - 使用
typings install --global --save dt~jquery安装类型和 jquery 定义 - 在 test.ts
/// <reference path="../../../typings/globals/jquery/index.d.ts" />顶部添加 jquery 引用
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");
【问题讨论】:
-
如何编译项目?此外,您的
tsconfig.json定义似乎是错误的。你不能同时使用exclude和files(files在这种情况下会获胜,所以路径可能是错误的)。如果使用了tsconfig.json,你也不需要/// <reference -
您可以参考下面的链接了解此解决方案的使用 $ 并查询打字稿中的所有功能。链接:c-sharpcorner.com/article/typescript-with-jquery
标签: jquery typescript typescript-typings