将jQuery与Typescript 2结合使用 [英] Using jQuery with Typescript 2
问题描述
我是Typscript 2的新手.我想做的是在Typescript中使用jQuery.在这个问题中,我读到您需要两件事:
I'm new to Typscript 2. What i'm trying to do is to use jQuery within typescript. In this question i read that you need two things:
npm install --save-dev @types/jquery
这将安装软件包'@ types/jquery 2.0.39'.
This installs package '@types/jquery 2.0.39'.
"devDependencies": {
"@types/jquery": "^2.0.39",
"typescript": "^2.0.2",
"typings": "^1.0.4"
}
然后,在Typescript文件中输入以下内容:
Then, in the Typescript file i put this:
import $ from "jquery";
但是我收到打字稿错误找不到模块'jquery'.我在做什么错了?
but i got typescript error 'Cannot find module 'jquery'. What am I doing wrong?
相同错误
import $ = require("jquery");
完整的打字稿文件:
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本身:
You only install jquery's typings. You will need jQuery itself, too:
npm install jquery --save
此外,由于您使用typescript @ 2,因此不再需要typings
软件包.现在将通过npm和@types
包(在您的情况下为@types/jquery
:
Furthermore, since you use typescript@2, you don't need the typings
package anymore. This will now be handled via npm and the @types
packages, available at $types/{your-package}
, in your case @types/jquery
:
npm install @types/jquery --save
这篇关于将jQuery与Typescript 2结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!