我是TypeScript的新手,我想使用 TypeScript版本0.9.0和Visual Studio 为我的代码使用多个文件。我认为我做了正确的代码,而IntelliSense似乎也是如此,但是当我实际运行它时失败,抛出JavaScript未定义的异常。
我有两个.ts文件,分别是app.ts和module.ts,这是我的短代码。
module.ts就在这里:
module Shapes {
export class Rectangle {
constructor(
public height: number,
public width: number) {
}
}
}
和app.ts就在这里:
/// <reference path="app/classes/module.ts" />
var rect = new Shapes.Rectangle(10, 4);
IntelliSense正确检测'Shapes'是什么,什么是'Shapes.Rectangle',但是当我运行此代码时,错误显示'Shapes'未定义。
所以我在网上搜索了一些文章,包括this和this,我按照他们的提示,但我失败了。我不明白为什么......
这是我的default.htm代码。
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
<link rel="stylesheet" href="app.css" type="text/css" />
<script src="app/classes/module.js"></script>
<script src="app.js"></script>
</head>
<body>
<h1>TypeScript HTML App</h1>
<div id="content"></div>
</body>
</html>
我认为我正确地将module.js添加到HTML文件中。任何人都可以帮助我吗?
答案 0 :(得分:1)
我认为你的问题很可能是module.js实际上没有被加载。大多数浏览器都包含一些用于查看网络流量的调试工具,但我更喜欢Fiddler。它独立于浏览器并且非常强大。
由于您使用的是内部模块(我们也选择了这种模块),您可能需要考虑将源代码编译为单个文件。它减少了网络流量,保持文件系统整洁,HTML文件更简单。为此,请在文本编辑器中打开csproj文件,查找包含PropertyGroups
的{{1}}并添加<TypeScriptTarget>
,其中<TypeScriptOutFile>outputfile.js</TypeScriptOutFile>
是您要生成的文件的名称
以下是我的例子:
outputfile.js