TS-2304错误-导入"jquery"时,在TypeScript中找不到名称"Iterable"在".ts"中文件 [英] TS-2304 Error - can not find name 'Iterable' in TypeScript while importing "jquery" in ".ts" file

查看:210
本文介绍了TS-2304错误-导入"jquery"时,在TypeScript中找不到名称"Iterable"在".ts"中文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio Code作为编辑器来处理TypeScript 2.4版.我使用以下命令将jQuery与NPM一起安装:

I am working on TypeScript version 2.4 using Visual Studio Code as an editor. I installed jQuery with NPM using the following command:

npm install --save @types/jquery

然后我从

Then I downloaded the source code of jquery module from GitHub.

registrationpage.ts文件的代码如下:

import * as $ from 'jquery';
class Registration_Page {
    txtuname: string;
    Registration() {
        $('#table').hide;
        this.txtuname = ( <HTMLTextAreaElement> (document.getElementById('txtusername'))).value;
    }
}
window.onload = () => {
    $('#table').show;
    var bttnLogin = document.getElementById('submit');
    var obj = new Registration_Page();
    bttnLogin.onclick = function () {
        obj.Registration();
    }
}

index.html的代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="registrationpage.js"></script>

</head>
<body>
    <form id="form1">
    <div>
     <fieldset style="font-size: medium; color: #000000;">
        <legend style="background-color:#CCCCFF; font-size: larger;font-weight:bold">Registration Page in TypeScript</legend>
        <br />
        <b>UserName</b>
        <input id="txtusername" type="text" /><br />
        <br />

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input id="submit" type="button" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;

    </fieldset>
    </div>
    <div id="table">
        <table>
            <tr>
                <th>UserName</th>

            </tr>
        </table>
    </div>
    </form>
</body>
</html>

tsconfig.json文件:

{
    "compilerOptions": {
        "target": "es5",
        "noImplicitAny": true,
        "lib": [ "es2015", "dom" ]

    }

}

在CMD上编译代码时,出现以下错误:

When I compile the code on CMD , I get the following error:

错误TS2304:找不到名称Iterable

ERROR TS2304 : cannot find the name Iterable

请提出适当的解决方案.

Please suggest an appropriate solution.

推荐答案

tsconfig.json的配置正确.当目标设置为ES5并且包含库:es2015,es2015-iterable时,则支持Iterable. 关键是当我们通过命令tsc "filename"编译.ts文件时,编译器将忽略"tsconfig.json".另外,在使用外部模块时,您需要包括以下js文件以支持模块加载:

The configuration of tsconfig.json is correct. When the target is set to ES5 and libraries : es2015,es2015-iterable are included then Iterable is supported. The point is when we compile the .ts file by command tsc "filename" then "tsconfig.json" is ignored by the compiler. Also, while working with external modules, you need to include the following js files to support module loading:

https://unpkg.com/core-js/client/shim.min.js">

https://unpkg.com/systemjs@0.19.39/dist/system.src.js

最后,使用此命令编译文件

Lastly, Compile the file with this command

**tsc -p .**

此命令将不会忽略tsconfig.json文件.

This command will not ignore tsconfig.json file.

希望它会有所帮助.

这篇关于TS-2304错误-导入"jquery"时,在TypeScript中找不到名称"Iterable"在".ts"中文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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