繁体   English   中英

TS-2304错误 - 在“.ts”文件中导入“jquery”时,在TypeScript中找不到名称“Iterable”

[英]TS-2304 Error - can not find name 'Iterable' in TypeScript while importing “jquery” in “.ts” file

我正在使用Visual Studio Code作为编辑器处理TypeScript版本2.4。 我使用以下命令在NPM上安装了jQuery:

npm install --save @types/jquery

然后我从GitHub下载了jquery module的源代码。

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上编译代码时,我收到以下错误:

错误TS2304:找不到名称Iterable

请提出适当的解决方案。

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

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

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

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

**tsc -p .**

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

希望能帮助到你。

考虑安装节点类型定义。

npm i --save-dev @types/node

如果您使用Yarn而不是NPM

yarn add -D @types/node

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM