我正在用 jquery 测试 typescript,但是当我编译 test.ts 文件时,它总是给我一个错误提示:找不到名称 '$'。
我已经导入了 jquery 并添加了它的定义引用。
如果我使用 import $ = require("jquery")
在我的 test.ts
文件,执行 Cannot find module jquery
时会出现另一个错误“tsc
”编译。但是,JQuery 文件夹已存在于 node_modules 文件夹中。
有谁知道在 typescript 中使用 jquery 的正确方法是什么?
以下是我的步骤:
- 使用
npm install jquery --save
安装 jquery - 使用
typings install --global --save dt~jquery
安装类型和 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
然后在 app.module.ts
中:
import * as $ from 'jquery';
然后运行:
npm install @types/jquery@2.0.47
你应该准备好了。
关于jquery - Typescript 和 JQuery 编译错误 : Cannot find name '$' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283236/