在 Typescript 2 中使用 jQuery
Using jQuery with Typescript 2
我是 Typscript 2 的新手。我想做的是在打字稿中使用 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";
但我收到打字稿错误“找不到模块 'jquery'。我做错了什么?
与
同样的错误
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
安装基本 npm
。
npm install jquery
然后通过以下方式使用它:
import $ = require('jquery');
始终有效。
您只安装 jquery 的类型。您还需要 jQuery 本身:
npm install jquery --save
此外,由于您使用的是 typescript@2,因此您不再需要 typings
包。这现在将通过 npm 和 @types
包来处理,可在 $types/{your-package}
获得,在您的情况下 @types/jquery
:
npm install @types/jquery --save
Now I have errors in index.d.ts file, for instance: Line:
attr(attributeName: string, value: string|number|null): JQuery;
Error1: A parameter initializer is only allowed in a function or
constructor implementation. Error2: Type expected
不是对您原始问题的回答,而是对 follow-up 的回答,因为我无权回答您的评论:
我有同样的错误,作为解决方法,我从 jQuery 的 index.d.ts 中删除了有问题的行,并且能够在打字稿中使用 jQuery。我还不知道这是否有任何不需要的 side-effects,所以使用它需要您自担风险。
我安装了 Visual Studio 更新(工具 -> 扩展和更新)"Typescript 2.0.6 for Visual Studio 2015"。
另外,我安装了以下 NuGet 包:
- Microsoft.TypeScript.Compiler
- Microsoft.Typescript.MSBuild
然后我通过 npm 安装了 jQuery 和 jQuery typings:
npm install jquery @types/jquery --save
最后,在 tsconfig.json:
中:
"compilerOptions": {
... other compiler options ...
"typeRoots": [ "node_modules/@types/" ],
"types": ["jquery" ]
},
现在我可以构建项目并且 $
在 TypeScript 中被识别为 jQuery。
我是 Typscript 2 的新手。我想做的是在打字稿中使用 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";
但我收到打字稿错误“找不到模块 'jquery'。我做错了什么?
与
同样的错误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
安装基本 npm
。
npm install jquery
然后通过以下方式使用它:
import $ = require('jquery');
始终有效。
您只安装 jquery 的类型。您还需要 jQuery 本身:
npm install jquery --save
此外,由于您使用的是 typescript@2,因此您不再需要 typings
包。这现在将通过 npm 和 @types
包来处理,可在 $types/{your-package}
获得,在您的情况下 @types/jquery
:
npm install @types/jquery --save
Now I have errors in index.d.ts file, for instance: Line: attr(attributeName: string, value: string|number|null): JQuery; Error1: A parameter initializer is only allowed in a function or constructor implementation. Error2: Type expected
不是对您原始问题的回答,而是对 follow-up 的回答,因为我无权回答您的评论: 我有同样的错误,作为解决方法,我从 jQuery 的 index.d.ts 中删除了有问题的行,并且能够在打字稿中使用 jQuery。我还不知道这是否有任何不需要的 side-effects,所以使用它需要您自担风险。
我安装了 Visual Studio 更新(工具 -> 扩展和更新)"Typescript 2.0.6 for Visual Studio 2015"。
另外,我安装了以下 NuGet 包:
- Microsoft.TypeScript.Compiler
- Microsoft.Typescript.MSBuild
然后我通过 npm 安装了 jQuery 和 jQuery typings:
npm install jquery @types/jquery --save
最后,在 tsconfig.json:
中:
"compilerOptions": {
... other compiler options ...
"typeRoots": [ "node_modules/@types/" ],
"types": ["jquery" ]
},
现在我可以构建项目并且 $
在 TypeScript 中被识别为 jQuery。