在 HTML 文档中嵌入 TypeScript 代码 [英] Embed TypeScript code in an HTML document
问题描述
是否可以在网页中嵌入 TypeScript 代码?我想在脚本标签中嵌入 TypeScript 代码,就像这样(这样它会自动编译为 Javascript):
Is it possible to embed TypeScript code in a web page? I want to embed TypeScript code inside script tags, like this (so that it is automatically compiled to Javascript):
<script type = "text/typescript">
//TypeScript code goes here
</script>
推荐答案
实际上有几个项目可以让你像这样使用 TypeScript 代码 - TypeScript 编译,ts-htaccess.
Actually there are several projects that allow you to use TypeScript code like that - TypeScript Compile, ts-htaccess.
这里的问题是 .ts 代码应该编译成 JavaScript - 它可以在客户端(慢;整个 TSC 也应该加载到客户端)或服务器端(显然更快,并且在编译后的代码上利用缓存要容易得多).
The catch here is that .ts code should be compiled into JavaScript - it can be done either at client-side (slow; the whole TSC should be loaded into the client as well) or at server-side (obviously faster, and it's far easier to leverage the cache on the compiled code).
这篇关于在 HTML 文档中嵌入 TypeScript 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!