如何将 JQuery 导入 Typescript 文件? [英] How to import JQuery into a Typescript file?
问题描述
不需要导入.相反,我需要添加对文件顶部的引用.所以我的 WebAPI.js 的第一行应该是 ///<reference path ="../typings/jquery/jquery.d.ts"/>
而不是 import{ $ } 来自'../jquery-3.1.1';
No import was required. Instead, I needed to add a reference to the top of the file. So the first line of my WebAPI.js should have been /// <reference path ="../typings/jquery/jquery.d.ts"/>
instead of import { $ } from '../jquery-3.1.1';
我正在尝试导入 jQuery 以在 Typescript 文件中使用,但是我尝试的所有操作都收到了各种错误.我按照解决方案这里和这里,但没有任何运气.
I am trying to import jQuery to use in a Typescript file, but I am receiving a variety of errors with everything I try. I followed the solutions here and here, but without any luck.
{
"compilerOptions": {
"removeComments": true,
"preserveConstEnums": true,
"out": "Scripts/CCSEQ.Library.js",
"module": "amd",
"sourceMap": true,
"target": "es5",
"allowJs": true
}
WebAPI.js
import { $ } from '../jquery-3.1.1';
export class ExpenseTransaction extends APIBase {
constructor() {
super();
}
Get(): void {
let expenses: Array<Model.ExpenseTransaction>;
let self = this;
$.ajax({
url: this.Connection,
type: "GET",
contentType: "application/json",
dataType: "json",
success: function (data: any): void {
expenses = self.ConvertToEntity(data.value);
},
error: function (data: any): void { console.log(data.status); }
});
};
}
我也试过 import * as $ from '../jquery.3.1.1'
模块 jquery-3.1.1 没有导出成员 $
属性ajax不存在于类型(选择器:任何,上下文:任何)=>任何
推荐答案
不需要导入.相反,在文件顶部添加对 Typescript 定义文件的引用.所以 WebAPI.js
的第一行应该是
An import is not required. Instead, add a reference to the Typescript definition file the top of the file. So the first line of the WebAPI.js
should be
/// <reference path ="../typings/jquery/jquery.d.ts"/>
代替
import { $ } from '../jquery-3.1.1';
根据 DefinitelyTyped 维基:
TypeScript 声明文件是定义类型、函数的方式和外部第三方 JavaScript 库中的参数.通过使用TypeScript 代码中的声明文件将启用 Intellisense并针对您正在使用的外部库进行类型检查.
A TypeScript declaration file is way of defining the types, functions and parameters in an external third-party JavaScript library. By using a declaration file in your TypeScript code will enable Intellisense and type checking against the external library you are using.
jquery.d.ts
是 DefinitelyTyped 库 的一部分在 GitHub 上.绝对类型可以通过 NuGet 包管理器包含在 Visual Studio 项目中.
jquery.d.ts
is a part of the DefinitelyTyped Library found on GitHub. Definitely Typed can be include in Visual Studio projects via the NuGet Package Manager.
这篇关于如何将 JQuery 导入 Typescript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!