2016-02-05 88 views
12

我想學習和切換到角2我的應用程序,但我有打字稿的問題。問題是我無法使用它。在我目前的環境中,我不能使用編譯器。即時通訊現在不得不運行node.exe(node.js可執行文件),但不是完整的node.js應用程序,只是可執行文件,這意味着我無法運行npm。但角2被寫入打字稿中,因此需要在發送到瀏覽器之前將其編譯爲JavaScript。有沒有辦法我可以得到一個預編譯版本的角2(所以它在JavaScript中),所以我可以寫我的代碼在JavaScript中,讓一切都運行沒有編譯器?如果沒有的話,你是否可以通過node.exe可執行文件手動安裝和使用typescript編譯器。使用角度2沒有打字稿編譯器

要清楚這不是因爲我不想使用打字稿它是因爲在我目前的情況下我不能安裝打字稿。

+2

的[?是否可以使用ES5的JavaScript與角2代替打字本]可能的複製(http://stackoverflow.com/問題/ 30689094/is-it-it-it-use-es5-javascript-with-angular-2-instead-of-typescript) –

回答

17

Angular2可用於TypeScript,JavaScript和Dart。不需要使用TypeScript。


- 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

參見Is it possible to use ES5 JavaScript with Angular 2 instead of 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> 
+0

即時通訊討論角2本身的問題在角度2測試版中寫入腳本,因此它需要打字稿才能運行,即使我不需要使用打字稿 – Binvention

+0

不,它是用TypeScript寫的。 JS和Dart版本是從TypeScript自動生成的,它們可以在JS和Dart源代碼中使用。 –

+0

JS源代碼在哪裏?我無法找到任何地方 – Binvention

3

如果你想使用打字稿編寫你的應用程序,你需要一個transpiler:

  • 靜態。例如,tsc -w命令在運行中在後臺運行
  • 。直接在瀏覽器中使用typescript.js文件

這表示可以使用ES5編寫Angular2應用程序。下面是一個示例:

編輯

如果包括這些JavaScript文件來回Angular2(文件夾node_modules/angular2/bundles),有任何關於打字稿:

<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> 

這些文件被轉換成JavaScript。所以沒有必要有TypeScript。有了它們,您就可以僅使用ES6來實現您的應用程序。

要使用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> 
+0

是的,我會告訴OP至少給它一個鏡頭,TypeScript使它更容易,因爲IDE自動完成你 –

+0

我的問題是角2寫在打字稿,所以我不能使用角,因爲我無法安裝打字稿。如果我可以安裝打字稿我會的 – Binvention

+0

是的,Angular2是用TypeScript編寫的,但是當你包含相應的捆綁JavaScript文件時,你不需要TypeScript,因爲它們被編譯成ES5 –