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

时间:2017-05-04 12:46:31

标签: javascript jquery ajax typescript import

更新

无需导入。相反,我需要添加对文件顶部的引用。所以我的WebAPI.js的第一行应该是/// <reference path ="../typings/jquery/jquery.d.ts"/>而不是import { $ } from '../jquery-3.1.1';

我正在尝试导入jQuery以在Typescript文件中使用,但我收到了各种错误,我尝试了一切。我按照解决方案herehere,但没有任何运气。

tsconfig.json

{
    "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'

错误

  • Module jquery-3.1.1 has no exported member $
  • Property ajax does not exist on type (selector: any, context: any) => any

6 个答案:

答案 0 :(得分:27)

您必须根据打字稿documentation将其导入import * as $ from "jquery";,并将jquery's definition file模块定义为环境模块:

declare module "jquery" {
    export = $;
}

根据this

  

Ambient声明是您使用编译器做出的承诺。   如果这些在运行时不存在并且您尝试使用它们,那么事情就会在没有警告的情况下中断。

希望它有所帮助!

答案 1 :(得分:18)

不需要导入。而是在文件顶部添加对Typescript定义文件的引用。所以WebAPI.js的第一行应该是

/// <reference path ="../typings/jquery/jquery.d.ts"/> 

而不是

import { $ } from '../jquery-3.1.1';

根据DefinitelyTyped wiki:

  

TypeScript声明文件是定义类型和函数的方法   和外部第三方JavaScript库中的参数。通过使用   TypeScript代码中的声明文件将启用Intellisense   并对正在使用的外部库进行类型检查。

jquery.d.ts是GitHub上DefinitelyTyped Library的一部分。绝对可以通过NuGet包管理器在Visual Studio项目中包含Typed。

答案 2 :(得分:9)

执行此操作:使用npm安装jquery或与cdn一起使用

首先:

dinero_inicial = int(input("Con cuanto dinero empiezas? ")) # new variable used here
muestra = int(input("Tamaño de la muestra: "))
datos=list()
while muestra>0:
    dinero = dinero_inicial         # move some other initializations inside the outer loop
    dinero_apostado = 1
    tiradas = 0
    while dinero >= dinero_apostado:
        casilla=random.choices([0,1,2],[1,18,18])
        casilla=casilla.pop()
        if casilla == 1:
            dinero = dinero + dinero_apostado
            dinero_apostado = 1
        elif casilla != 1:
            dinero = dinero - dinero_apostado
            dinero_apostado = dinero_apostado * 2
        tiradas+=1
    datos.append(tiradas)
    muestra-=1
print(datos)

以及之后:

  

导入*作为来自&#39; jquery&#39;;

的$

答案 3 :(得分:1)

Tim使用参考编译器指令的解决方案有效,但是现在有一种更简单的方法。

在tsconfig.json中,如果设置typeRoots,则无需在.ts文件中进行任何操作。 TypeScript为您完成工作。

它如何工作?

在我的解决方案中,我通过npm输入所有@type,因此将它们放置在./node_modules/@types中。我还通过./@types手工创建了一些类型。

在我的tsconfig.json中,我添加了:

 "compilerOptions": {     
   // lots of other config here
   "typeRoots": [
     "./node_modules/@types",
     "./@types"
   ]
  }

现在我的所有类型都被编译器自动发现并使用,因此无需理会引用标记!

总结...

我应该注意,如果您这样做并且尝试显式导入jquery,它将失败,并且您会感到困惑。我确定是。

答案 4 :(得分:0)

尝试将导入替换为 声明let $:any;

答案 5 :(得分:0)

在Visual Studio社区中,要执行此工作,我必须在head / script部分的html中添加jquery,然后在jquery script部分的正下方添加我的app.ts,然后在我的打字稿源中添加对jquery lib的引用。 / p>

在html页面的头部:

<script src="./Scripts/jquery-3.5.1.js"></script>
<script src="app.js"></script>

在app.ts中,ts脚本的第一行:

/// <reference path ="./Scripts/typings/jquery/jquery.d.ts"/>