如何将JQuery导入到Typescript文件中? [英] How to import JQuery into a Typescript file?

查看:1435
本文介绍了如何将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文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆