简体   繁体   English

Angular2的System.config-使用JavaScript还是打字稿?

[英]System.config for Angular2 - use javascript or typescript?

I'm just wading into setting up an Angular2 app and I'm not sure what is the best way to configure the index.html. 我只是想要设置一个Angular2应用程序,我不确定配置index.html的最佳方法是什么。 I have two examples to go from: a javascript version and a typescript version. 我有两个例子:一个ja​​vascript版本和一个打字稿版本。 Since Angular2 is using typescript I thought the typescript version makes sense. 由于Angular2使用的是打字稿,我认为打字稿版本很有意义。 The javascript version is coming from the new Angular2 book from Ari Lerner. javascript版本来自Ari Lerner的新Angular2书籍。 Here are the 2 examples: 这是两个示例:

Typescript configuration: 打字稿配置:

System.config({
    transpiler: 'typescript',
    typescriptOptions: {emitDecoratorMetadata: true},
    packages: {app: {defaultExtension: 'ts'}}
});
System.import('app/app');

Javascript configuration: Javascript配置:

System.config({
  packages: {        
    app: {
      format: 'register',
      defaultExtension: 'js'
    }
  }
});
System.import('app/app.js')
    .then(null, console.error.bind(console));

My question is which one is the best to use, and why? 我的问题是哪一个最好用,为什么?

Angular2 app can be written with Typescript Or ES5/ES6 . Angular2应用程序可以使用TypescriptES5 / ES6编写。 While learning angular2, you must have found that angular2 website provides two kinda docs (actually three), check now if you haven't (you will be able to see Angular2 for Typescript , Angular2 for Javascript , Angular2 for Dart . 在学习angular2时,您必须已经找到angular2网站提供了两种文档(实际上是三篇),现在检查是否还没有(可以看到Angular2 for TypescriptAngular2 for JavascriptAngular2 for Dart

Now it is up to you for which platform you are going to write your Angular2 app. 现在由您决定要在哪个平台上编写Angular2应用程序。

1) . 1) If you plan to write angular2 app with typescript , obviously your targeted web browser won't understand it and so some mechanism has to convert typescript code into javascript that can be understood by targeted web-browswer. 如果你打算写与angular2应用typescript ,显然您的目标Web浏览器将无法理解它,所以一些机构有打字稿代码转换为javascript ,可以通过有针对性的网络browswer理解。 So first snippet of code means convert/transpile .ts file into .js so your browser will be to understand. 因此,第一段代码意味着将.ts文件转换/转换为.js以便您的浏览器能够理解。


2) . 2) If you plan to write angular2 app with Javascript/ES5/ES6 then of course, second part of your question. 如果您打算使用Javascript/ES5/ES6编写angular2应用程序,那么当然是问题的第二部分。

Suggestion : Go with first as Angular2 itself has been written in typescript from the scratch . 建议首先,因为Angular2本身是从头开始用打字稿编写的

First, I think that there is a small mistake in the JavaScript configure: 首先,我认为JavaScript配置中存在一个小错误:

System.config({
  packages: {        
    app: {
      format: 'register',
      defaultExtension: 'js'
    }
  }
});
System.import('app/app'); // <------
  .then(null, console.error.bind(console));

Regarding your question: 关于您的问题:

I think that first configuration (transpiling on the fly) is fine for small applications where performances dosn't matter . 我认为,对于性能无关紧要的小型应用程序,第一种配置(即时转换)是合适的。 As a matter of fact, there are some additional processing (in the browser) to transpile the application code into something executable by the browser at the level of module loading. 事实上,还有一些额外的处理(在浏览器中)将应用程序代码转换为浏览器在模块加载级别可执行的内容。 Such approach is fine for applications executed in plunkr. 这种方法适用于在plunkr中执行的应用程序。

I would say that the JavaScript configuration that leverages pre compilation of TypeScript content is more efficient since the browser directly execute module code in JavaScript (no transpiling done by the browser itself). 我会说利用TypeScript内容预编译的JavaScript配置更有效,因为浏览器直接在JavaScript中执行模块代码(浏览器本身没有完成转换)。

That said, if you want to have something really efficient (production read application), you need to package your application: 就是说,如果您想要一个真正有效的东西(生产读取应用程序),则需要打包您的应用程序:

  • 1) to precompile your TypeScript code into JavaScript 1)将TypeScript代码预编译为JavaScript
  • 2) to gather JavaScript code into a minimum set of JS files to minimize the number of files to load 2)将JavaScript代码收集到最小的JS文件集中,以最大程度地减少要加载的文件数
  • 3) to minify JavaScript code to reduce its size 3)缩小JavaScript代码以减小其大小

The first approach doesn't allow all these three points. 第一种方法不允许所有这三点。 The second approach allows precompilation and minification but you can't gather your whole application code into a single file. 第二种方法允许预编译和最小化,但是您不能将整个应用程序代码收集到单个文件中。

To do that you need to leverage the outFile parameter of the tsc compiler. 为此,您需要利用tsc编译器的outFile参数。 This way you will have all modules into a single file that you can minify. 这样,您会将所有模块都放在一个可以缩小的文件中。 In this case, you don't need anymore to configure SystemJS. 在这种情况下,您不再需要配置SystemJS。 You need to import the main module... 你需要导入主模块......

These questions could give you additional hints: 这些问题可以为您提供额外的提示:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM