Javascript 在没有 TypeScript 转译器的情况下使用 Angular 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35229611/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:25:52  来源:igfitidea点击:

Using Angular 2 without TypeScript Transpiler

javascriptnode.jsangular

提问by Binvention

I want to learn Angular 2 and switch my app to using it, however, I'm having a problem with using TypeScript.

我想学习 Angular 2 并将我的应用程序切换到使用它,但是,我在使用 TypeScript 时遇到了问题。

In my current environment I can't use a use the transpiler / compiler. I'm currently having to run off of just the node.js executable (node.exe) but not the full node.js application, nor npm. Angular 2 is written in TypeScript, so it needs to be compiled to JavaScript before being sent to the browser.

在我当前的环境中,我无法使用转译器/编译器。我目前只需要运行 node.js 可执行文件 (node.exe) 而不是完整的 node.js 应用程序,也不是 npm。Angular 2 是用 TypeScript 编写的,所以在发送到浏览器之前需要编译成 JavaScript。

Is there a way to get a pre-compiled version of Angular 2 so I can write in JavaScript and run without a compiler?

有没有办法获得 Angular 2 的预编译版本,这样我就可以用 JavaScript 编写并在没有编译器的情况下运行?

If not, is there any way to manually install and use the TypeScript compiler with just the node.exe executable?

如果没有,是否有任何方法可以仅通过 node.exe 可执行文件手动安装和使用 TypeScript 编译器?

To be clear, this isn't because I don't want to use TypeScript, but rather because in my current situation I can't install it.

需要明确的是,这不是因为我不想使用 TypeScript,而是因为在我目前的情况下我无法安装它。

回答by Günter Z?chbauer

Angular2 is available in TypeScript, JavaScript and Dart. No need to use TypeScript.

Angular2 在 TypeScript、JavaScript 和 Dart 中可用。无需使用 TypeScript。

See
- https://angular.io/docs/js/latest/index.html
- http://blog.thoughtram.io/angular/2015/05/09/writing-angular-2-code-in-es5.html
- http://blog.thoughtram.io/angular/2015/07/06/even-better-es5-code-for-angular-2.html


- https://angular.io/docs/js/latest/index.html
- http://blog.thoughtram.io/angular/2015/05/09/writing-angular-2-code-in-es5。 html
- http://blog.thoughtram.io/angular/2015/07/06/even-better-es5-code-for-angular-2.html

See also Is it possible to use ES5 JavaScript with Angular 2 instead of TypeScript?

另请参阅是否可以在 Angular 2 中使用 ES5 JavaScript 而不是 TypeScript?

<script src="https://code.angularjs.org/2.0.0-beta.3/Rx.umd.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.3/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.3/angular2-all.umd.dev.js"></script>

回答by Thierry Templier

If you want to use TypeScript to write your application, you need a transpiler:

如果你想使用 TypeScript 来编写你的应用程序,你需要一个转译器:

  • static. For example with a tsc -wcommand running in background
  • on the fly. Directly within the browser using the typescript.js file
  • 静止的。例如tsc -w在后台运行的命令
  • 在飞行中。直接在浏览器中使用 typescript.js 文件

That said it's possible to write Angular2 applications with ES5 only. Here is a sample:

也就是说,可以仅使用 ES5 编写 Angular2 应用程序。这是一个示例:

Edit

编辑

When you include these JavaScript files fro Angular2 (folder node_modules/angular2/bundles), there is nothing about TypeScript:

当您包含来自 Angular2(文件夹node_modules/angular2/bundles)的这些 JavaScript 文件时,TypeScript 没有任何内容:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

These files were transpiled into JavaScript. So there is no need to have TypeScript. With them, you can implement your application with ES6 only.

这些文件被转换成 JavaScript。所以没有必要有 TypeScript。有了它们,您只能使用 ES6 实现您的应用程序。

To use ES5 only, you need to include these ones:

要仅使用 ES5,您需要包含以下内容:

<script src="node_modules/angular2/bundles/Rx.umd.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/angular2/bundles/angular2-all.umd.dev.js"></script>