我正在使用Visual Studio Code作爲編輯器的TypeScript版本2.4。我安裝jQuery的與NPM使用下面的命令:TS-2304錯誤 - 在「.ts」文件中導入「jquery」時無法在TypeScript中找到名稱'Iterable'
npm install --save @types/jquery
然後我下載的jquery module
源代碼從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 />
<input id="submit" type="button" value="Submit" />
</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上,我得到以下錯誤:
ERROR TS2304 : cannot find the name Iterable
請建議適當的解決方案。
你找到一個解決的辦法,我有同樣的問題 –
我在得到這個同樣的問題VS 2017 UI。從你的答案下面,它聽起來像tsconfig.json被忽略?你能提供更多細節嗎?如何解決這個問題? –
是的,它忽略了tsconfig.json文件。解決方案是使用命令編譯代碼:「tsc -p」。 請參考:「https://www.typescriptlang.org/docs/handbook/tsconfig-json.html」 – Swinky