如何将JQuery导入到Typescript文件中?

编程入门 行业动态 更新时间:2024-10-25 10:21:58
本文介绍了如何将JQuery导入到Typescript文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无需导入。相反,我需要添加对文件顶部的引用。所以我的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文件中?

本文发布于:2023-08-02 15:25:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   导入到   文件   JQuery   Typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!