在 cshtml 文件中使用 TypeScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12695451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using TypeScript in cshtml files
提问by Artyom Krivokrisenko
I'm wondering is there any way to use TypeScript on Razor cshtml files?
我想知道有没有办法在 Razor cshtml 文件上使用 TypeScript?
For example, something like this
例如,这样的事情
<script language="text/typescript">
/// typescript goes here
</script>
采纳答案by Rob
TypeScript isn't a runtime; it's cross-compiled into JavaScript. As a result, you'll need to write your TypeScript, compile it, and then either include it within JavaScript script tags or as an external file.
TypeScript 不是运行时;它被交叉编译成 JavaScript。因此,您需要编写 TypeScript,对其进行编译,然后将其包含在 JavaScript 脚本标记中或作为外部文件包含在内。
回答by niutech
It's possible. I have developed TypeScript Compile- an automatic compiler of TypeScript to JavaScript on the fly. Have a try!
这是可能的。我开发了TypeScript Compile- 一种自动将 TypeScript 即时编译为 JavaScript。试试!
回答by Manuel Schweigert
I just checked with my favorite VS Extension: Web Essentials
我刚刚检查了我最喜欢的 VS 扩展:Web Essentials
They already included .ts file compilation on saving (it is recommended to also use the original plugin for Intellisense).
他们已经在保存时包含了 .ts 文件编译(建议也使用 Intellisense 的原始插件)。
This obviously only works for .ts files, though. In my opinion, once you reach the complexity to choose typescript over javascript, you should use it in a separate file, anyways.
不过,这显然只适用于 .ts 文件。在我看来,一旦你达到选择 typescript 而不是 javascript 的复杂性,无论如何你应该在一个单独的文件中使用它。
回答by Michael Sondergaard
Let me add to Robs answer that it's technically possible to embed the typescript compiler in a page download, and have the browser compile code written in <script language="text/typescript">tags.
让我补充一下 Robs 的回答,在技术上可以将 typescript 编译器嵌入到页面下载中,并让浏览器编译用<script language="text/typescript">标签编写的代码。
Performance however, would be suboptimal and precompilation on the server would be preferred. Technically, there's nothing preventing a preprocessor from doing this either (T4 could do it).
然而,性能将是次优的,最好在服务器上进行预编译。从技术上讲,也没有什么可以阻止预处理器执行此操作(T4 可以执行此操作)。
回答by Wouter Devinck
You could manually compile the TypeScript files using tsc.exe and then add the resulting Javascript to your project or use a tool, such as Web Essentialsthat compiles on save.
您可以使用 tsc.exe 手动编译 TypeScript 文件,然后将生成的 Javascript 添加到您的项目或使用工具,例如在保存时编译的Web Essentials。
As the compiler can be compiled to Javascript, you can also let the user's browser do the compilation on the fly (at the cost of performance and file size, the compiler is fairly big). An example of this approach is niutech's solution.
由于编译器可以编译成Javascript,你也可以让用户的浏览器即时编译(以性能和文件大小为代价,编译器相当大)。这种方法的一个例子是 niutech 的解决方案。
If you are using Bundling and Minification, I have just released an implementation of IBundleTransform that compiles TypeScript to Javascript. It is on GitHuband NuGet (Install-Package TypeScriptBundleTransform). If you are not yet using Bundling and Minification, it is worth a look!
如果您正在使用 Bundling 和 Minification,我刚刚发布了一个 IBundleTransform 的实现,它将 TypeScript 编译为 Javascript。它位于GitHub和 NuGet (Install-Package TypeScriptBundleTransform) 上。如果您还没有使用 Bundling 和 Minification,那么值得一看!

