无需导入。相反,我需要添加对文件顶部的引用。所以我的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';根据 DefinitelyTyped wiki:
TypeScript声明文件是定义类型的函数,函数和外部第三方JavaScript库中的参数。通过在TypeScript代码中使用a声明文件,将启用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项目中包含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文件中?
发布评论