Typescript和JQuery编译错误:找不到名称"$" [英] Typescript and JQuery compile error: Cannot find name '$'

查看:713
本文介绍了Typescript和JQuery编译错误:找不到名称"$"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用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?

以下是我的步骤:

  1. 使用npm install jquery --save
  2. 安装jquery
  3. 安装键入&使用typings install --global --save dt~jquery
  4. 定义jquery
  5. 在test.ts /// <reference path="../../../typings/globals/jquery/index.d.ts" />
  6. 顶部添加jquery引用
  1. Install jquery using npm install jquery --save
  2. Install typings & jquery definition using typings install --global --save dt~jquery
  3. 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/jquery@2.0.47

您应该准备出发了.

这篇关于Typescript和JQuery编译错误:找不到名称"$"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆