简体   繁体   English

在Visual Studio中使用打字稿时,为什么需要在default.htm中包含多个.js文件?

[英]Why do I need to include multiple .js files in default.htm when working with typescript in visual studio?

I'm just starting out with typescript. 我只是从打字稿开始。 I wanted to try working with this mankala example from within Visual Studio. 我想尝试在Visual Studio中使用此mankala示例 Eventually I got it working but I had to include all of the .js files that were generated from .ts files in my default.htm file. 最终,我使它工作了,但是我必须在default.htm文件中包括从.ts文件生成的所有.js文件。 The .htm file in the example only included one file - the one that contained the entry point. 该示例中的.htm文件仅包含一个文件-包含入口点的文件。 I'm guessing that there's something set wrong in my configuration that I'm compensating for by the multiple .js includes. 我猜我的配置中有些错误,我要通过多个.js包含项来弥补。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

More details follow... 更多详细信息如下...

Here's what the original .htm file looked like: 这是原始.htm文件的样子:

<!DOCTYPE html>
<html>

<head>
  <title>Mankala</title>
  <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  <script type="text/javascript" src="game.js"></script>
  <link rel="stylesheet" type="text/css" href="play.css"/>  
</head>
<body id="bod" onload="Mankala.testBrowser()">
   <div class="hscore">Human: <span id="humscore">0</span></div>
   <div class="cscore">Computer: <span id="compscore">0</span></div>
</body>
</html>

And this is what my modified .htm file looked like: 这就是我修改后的.htm文件的样子:

<!DOCTYPE html>
<html>

<head>
  <title>Mankala</title>
  <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  <script type="text/javascript" src="base.js"></script>
  <script type="text/javascript" src="Driver.js"></script>
  <script type="text/javascript" src="Features.js"></script>
  <script type="text/javascript" src="Game.js"></script>
  <script type="text/javascript" src="geometry.js"></script>
  <script type="text/javascript" src="Position.js"></script>
  <link rel="stylesheet" type="text/css" href="play.css"/>  
</head>
<body id="bod" onload="Mankala.testBrowser()">
   <div class="hscore">Human: <span id="humscore">0</span></div>
   <div class="cscore">Computer: <span id="compscore">0</span></div>
</body>
</html>

To create the project I created a default ( not quite empty ) typescript project, deleted the automatically created app.ts file from that project and then added 6 new .ts files with the same names as the .ts files in the example. 为了创建项目,我创建了一个默认的(不是很空的)打字稿项目,从该项目中删除了自动创建的app.ts文件,然后添加了6个与示例中的.ts文件名称相同的新.ts文件。 Then I copied the .ts files from the example over the new .ts files that were created by VS. 然后,将示例中的.ts文件复制到VS创建的新.ts文件上。 I replaced the automatically generated app.css file with the play.css file from the example and replaced the contents of the automatically generated default.htm file with the contents of the play.htm from the sample. 我将示例中的play.css文件替换为自动生成的app.css文件,并将示例中的play.htm内容替换为自动生成的default.htm文件。 This didn't run but after I added the additional .js files to default.htm it did. 这没有运行,但是在我将其他.js文件添加到default.htm之后,它确实运行了。

I'm using Visual Studio 2012 Express for the Web and the typescript 0.8.3.1 VS extension. 我将Visual Studio 2012 Express用于Web和打字稿0.8.3.1 VS扩展。 I'm using the Chrome browser on windows7. 我正在Windows7上使用Chrome浏览器。

The difference here is how the compiler got invoked. 此处的区别在于编译器的调用方式。

When you build the Mankala sample, assuming you read the README, you ran 在构建Mankala示例时,假设您已阅读自述文件,

tsc Driver.ts -out game.js

The -out flag tells the compiler to concatenate the compilation into one big .js file. -out标志告诉编译器将编译链接到一个大的.js文件中。 However, the default behavior in Visual Studio projects is to build side-by-side, ie base.ts creates base.js , Features.ts creates Features.js , etc. 但是,Visual Studio项目的默认行为是并排构建,即base.ts创建base.jsFeatures.ts创建Features.js等。

You can fix your project file by adding a <TypeScriptOutFile>game.js</TypeScriptOutFile> element to the project in the same <PropertyGroup> as the other TypeScript settings (near line 57 in a default new project). 您可以通过将<TypeScriptOutFile>game.js</TypeScriptOutFile>元素添加到与其他TypeScript设置相同的<PropertyGroup>的项目中来修复项目文件(默认情况下,在新项目的第57行附近)。 Be sure to add to both the Debug and Release PropertyGroup elements if you want the same behavior in both compilation settings, or create a new non-conditional PropertyGroup . 如果要在两个编译设置中都具有相同的行为,请确保同时添加到DebugRelease PropertyGroup元素,或者创建一个新的无条件PropertyGroup

暂无
暂无

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

相关问题 自定义dojo小部件未在default.htm上加载 - Custom dojo widgets not loading on default.htm 为什么在 typescript 中导入文件时需要添加 .js 扩展名,而在 Angular 导入中不需要? - Why do I need to add the .js extension when importing files in typescript but not in Angular imports? 如何使Visual Studio自动包含从Typescript编译的JS文件? - How to get Visual Studio to automatically include JS files compiled from Typescript? 我是否需要在灯箱页面中包含CSS和JS文件? - Do I need to include css and js files in the lightbox page? 在Visual Studio网站中调试多个htm页面 - debug multiple htm pages in visual studio website 使用ExtJs日历需要包括哪些js文件? - What js files do I need to include to use ExtJs Calendar? 在 Visual Studio 打字稿编译中隐藏 .js 和 .js.map 文件 - Hiding .js and .js.map files in Visual Studio typescript compilation Visual Studio 代码断点在使用 TypeScript 的 Node.js 上不起作用 - Visual Studio code breakpoint not working on Node.js using TypeScript 如何在使用 Visual Studio 2019 制作的 Django 项目中添加 TypeScript 文件 - How do I add TypeScript files in a Django project made with Visual Studio 2019 当我在Visual Studio 2017中使用Node.js交互式窗口时,为什么会出现“SyntaxError:Unexpected identifier”错误? - Why do I get a “SyntaxError: Unexpected identifier” error when I use the Node.js Interactive Window in Visual Studio 2017?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM