我的应用中有三个TypeScript文件。当我构建时,它们编译得很好,Gulp将它们放在正确的位置,然后应用程序崩溃,因为我得到了:
ReferenceError:未定义require
对于TS编译输出的三个JS文件中的每一个。现在我不能很好地告诉tsc
"不要使用require
"我可以吗?
当我添加引用
时<reference path="~/wwwroot/js/system.src.js" />
到gulpFile.js,然后require
在该文件中工作,但是在我的构建上运行,而不是在运行时在浏览器上运行。如果我将它添加到其中一个已编译的JS文件中,它就不起作用。
答案 0 :(得分:2)
你需要包含一些文件&#34;手动&#34;在加载应用程序之前,使用index.html或您使用的任何(即cshtml Razor模板)。它应该看起来像这样
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="~/lib/core-js/client/shim.min.js"></script>
<script src="~/lib/zone.js/dist/zone.js"></script>
<script src="~/lib/reflect-metadata/Reflect.js"></script>
<script src="~/lib/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="~/app/system.config.js" asp-append-version="true"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
订单很重要。这是运行应用程序所必需的。
如果您在Visual Studio中收到错误,那么您可能会错过所需的&#34;打字&#34;。
到目前为止,您需要安装typings(从nodejs页面安装nodejs以使其在shell / powershell /命令行中全局可用)然后运行npm install typings -g
以安装打字并切换到您的文件夹项目运行typings install dt~node
,typings install dt~jasmine
和typings install dt~core-js
。这将在typings
文件夹中安装输入文件。
使用TypeScript 2.0,有一个更新的类型定义通过npm安装类型定义w / o需要打字(参见TypeScript 2.0声明here),运行以下命令:npm install -s @types/<nameofthelibrary>
,即{ {1}},npm install -s @types/node
和npm install -s @types/jasmine
。
npm install -s @types/core-js
方法在require
包/ typings中定义,如节点类型中index.d.ts文件中的node
所示。