我是 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/