在浏览器中运行 TypeScript 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13031518/
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
Running TypeScript project in the browser
提问by Arrow
I found out about TypeScript, and installed the Visual Studio 2012 Plugin, downloaded the source codeand ran the first sampleafter referencing the .ts
file in my HTML
document:
我发现了TypeScript,并安装了Visual Studio 2012 Plugin,下载了源代码并在引用我文档中的文件后运行了第一个示例:.ts
HTML
function Greeter(person) {
return "Hello, " + person + ".";
}
var user = "James Kent";
document.body.innerHTML = Greeter(user);
And then proceeded to compile the code at the command-line, with:
然后继续在命令行编译代码,使用:
tsc greeter.ts
tsc greeter.ts
But I could not compile it, as Visual Studio says:
但我无法编译它,正如 Visual Studio 所说:
Command "tsc" is not valid.
命令“tsc”无效。
After looking all over the TypeScript website, I was unable to find any information about how to get it working. A Google search also yielded no relevant results.
在浏览了 TypeScript 网站后,我找不到任何有关如何使其工作的信息。谷歌搜索也没有产生相关结果。
How can I get TypeScript working?
我怎样才能让 TypeScript 工作?
Update:Running any of the samples provided with the source code simply displays a blank page in any browser. However, samples appear to work just fine on the TypeScript website.
更新:运行随源代码提供的任何示例只会在任何浏览器中显示一个空白页面。但是,示例在 TypeScript 网站上似乎运行良好。
采纳答案by Roy
Your system cannot find the path to the compiler. The compiler executable is here (on my x64 Win 8 system) if you want to register it yourself.
您的系统找不到编译器的路径。如果您想自己注册,则编译器可执行文件在这里(在我的 x64 Win 8 系统上)。
C:\Program Files (x86)\Microsoft SDKs\TypeScriptnpm install -g typescript
.8.0.0\
Use it to compile the .ts file to a .js, and use that in your html instead of trying to compile it directly in the browser. That would be really slow.
使用它将 .ts 文件编译为 .js,并在您的 html 中使用它,而不是尝试直接在浏览器中编译它。那真的会很慢。
You can look at the "HTML Application with TypeScript" project in VS, it is configured to compile your TypeScript at the project build.
您可以查看 VS 中的“HTML Application with TypeScript”项目,它被配置为在项目构建时编译您的 TypeScript。
回答by Valentin
A simple way to get tsc
working on the command line is using nodejs
and the corresponding package manager npm
. You can get them from nodejs.org. Once you have set up nodejs
, all you have to do is install the compiler with
tsc
在命令行上工作的一种简单方法是使用nodejs
和相应的包管理器npm
。您可以从nodejs.org获取它们。设置完成后nodejs
,您所要做的就是安装编译器
Executing Typescript directly in the browser is rather complicated. Typescript compiles to Javascript, so what you want is reference the compiled code in your html
.
直接在浏览器中执行 Typescript 相当复杂。Typescript 编译为 Javascript,所以你想要的是在你的html
.