jquery - 将 jQuery 与 Typescript 2 结合使用

标签 jquery typescript2.0

我是 Typscript 2 的新手。我想做的是在 typescript 中使用 jQuery。在 this我读到的问题是你需要两件事:

npm install --save-dev @types/jquery

这将安装包“@types/jquery 2.0.39”。

"devDependencies": {
    "@types/jquery": "^2.0.39",
    "typescript": "^2.0.2",
    "typings": "^1.0.4"
  }

然后,在 Typescript 文件中我放入以下内容:

import $ from "jquery";

但我收到 typescript 错误“找不到模块‘jquery’”。我做错了什么?

同样的错误

import $ = require("jquery");

完整的 typescript 文件:

import { Component } from '@angular/core';
import $ from "jquery";

@Component({
  selector: 'my-app',
  template: `<div id="test"></div>`,
})
export class AppComponent  {
    constructor() {
        $('#test').html('This is a test');
    }
}

最佳答案

您只安装 jquery 的类型。您还需要 jQuery 本身:

npm install jquery --save

此外,由于您使用 typescript@2,因此不再需要 typings 包。现在,这将通过 npm 和 @types 包进行处理,在 $types/{your-package} 中可用,在您的情况下是 @types/jquery:

npm install @types/jquery --save

关于jquery - 将 jQuery 与 Typescript 2 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826503/

相关文章:

jQuery 2.1.0 |将 BR 标记添加到包含未给出预期结果的文本的按钮

javascript - 如何执行绑定(bind)到事件的监听器并从执行中排除绑定(bind)到不同事件的其余监听器?

javascript - 在调整窗口大小时设置产品 slider 宽度以及 CSS 媒体查询

angular - 如何将对象推送到 angular2 和 typescript 中的数组?

javascript - TS2511 : Cannot create an instance of the abstract class 'Validator'

typescript - 多个 tsconfig.json - 引用其他类型,但不包含它们与编译

jquery - Bootstrap datepicker的index.html具体例子

javascript - 在 TypeScript tsconfig.json 文件中使用 typeRoots 与 baseUrl 的相对路径

javascript - TypeScript 和运行时类型检查,2020 年的简单解决方案

javascript - 为什么使用 jQuery 组件的函数调用会重新加载 xhtml 页面?