问题描述
无需导入。相反,我需要添加对文件顶部的引用。所以我的WebAPI.js的第一行应该是 ///< reference path =../ typings / jquery / jquery.d.ts/>
代替从'../ 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
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不存在on type(selector:any,context:any)=>任何
Module jquery-3.1.1 has no exported member $
Property ajax does not exist on type (selector: any, context: any) => any
推荐答案
不需要导入。而是在文件顶部添加对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';
根据 wiki:
jquery.d.ts
是在GitHub上找到。绝对可以通过NuGet包管理器在Visual Studio项目中包含Typed。
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文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!