| 
									
										
										
										
											2016-08-11 11:29:19 -07:00
										 |  |  | extends ../../ts/_cache/quickstart.jade | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | block includes | 
					
						
							|  |  |  |   include _util-fns | 
					
						
							|  |  |  |   - var _Install = 'Get' | 
					
						
							|  |  |  |   - var _prereq = 'the Dart SDK' | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |   - var _angular_browser_uri = 'angular2/platform/browser.dart' | 
					
						
							|  |  |  |   - var _angular_core_uri = 'angular2/core.dart' | 
					
						
							|  |  |  |   - var _stepInit = 3 | 
					
						
							|  |  |  |   - var _quickstartSrcURL='https://github.com/angular-examples/quickstart' | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | block setup-tooling | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     Install the **[Dart SDK](https://www.dartlang.org/downloads/)**, | 
					
						
							|  |  |  |     if not already on your machine, and any tools you like to use with Dart. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     The Dart SDK includes tools such as **[pub][pub]**, the Dart package manager. | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     If you don't have a favorite Dart editor already, try | 
					
						
							|  |  |  |     [WebStorm][WS], which comes with a Dart plugin. | 
					
						
							|  |  |  |     You can also download [Dart plugins for other IDEs and editors][DT]. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     [WS]: https://confluence.jetbrains.com/display/WI/Getting+started+with+Dart | 
					
						
							| 
									
										
										
										
											2016-07-06 14:39:14 -07:00
										 |  |  |     [DT]: https://www.dartlang.org/tools/ | 
					
						
							|  |  |  |     [pub]: https://www.dartlang.org/tools/pub/ | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block package-and-config-files | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     In the project folder just created, create a file named | 
					
						
							|  |  |  |     **[pubspec.yaml][pubspec]** with the code below. | 
					
						
							|  |  |  |     This pubspec must specify the **angular2** and **browser** | 
					
						
							|  |  |  |     packages as dependencies, as well as the `angular2` transformer. | 
					
						
							|  |  |  |     It can also specify other packages and transformers for the app to use, | 
					
						
							|  |  |  |     such as [dart_to_js_script_rewriter](https://pub.dartlang.org/packages/dart_to_js_script_rewriter). | 
					
						
							| 
									
										
										
										
											2016-09-07 16:52:04 -07:00
										 |  |  |     Angular 2 is still changing, so provide an exact version: **2.0.0-beta.21**. | 
					
						
							| 
									
										
										
										
											2015-03-03 11:32:06 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     [pubspec]: https://www.dartlang.org/tools/pub/pubspec.html | 
					
						
							| 
									
										
										
										
											2015-03-02 14:46:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |   +makeExample('quickstart/dart/pubspec.yaml', null, 'pubspec.yaml') | 
					
						
							| 
									
										
										
										
											2015-03-03 11:32:06 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block install-packages | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     From the project folder, run `pub get` to install the angular2 and browser | 
					
						
							|  |  |  |     packages (along with the packages they depend on). | 
					
						
							| 
									
										
										
										
											2015-09-23 18:19:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 08:34:29 -07:00
										 |  |  |   code-example(language="sh" class="code-shell"). | 
					
						
							| 
									
										
										
										
											2015-09-23 18:19:14 -07:00
										 |  |  |     > <span class="blk">pub get</span> | 
					
						
							| 
									
										
										
										
											2015-10-15 23:25:02 -07:00
										 |  |  |     Resolving dependencies... | 
					
						
							| 
									
										
										
										
											2015-09-23 18:19:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block annotation-fields | 
					
						
							| 
									
										
										
										
											2015-11-10 18:31:46 +00:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2015-10-15 23:25:02 -07:00
										 |  |  |     The call to the `@Component` constructor has two | 
					
						
							|  |  |  |     named parameters, `selector` and `template`. | 
					
						
							| 
									
										
										
										
											2015-03-02 18:42:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block create-main | 
					
						
							| 
									
										
										
										
											2015-03-03 16:37:56 -08:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     Now we need something to tell Angular to load the root component. | 
					
						
							|  |  |  |     Create: | 
					
						
							|  |  |  |   ul | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     li a #[b folder named #[code web]] | 
					
						
							|  |  |  |     li a <b>file named #[code #[+adjExPath('app/main.ts')]]</b> with the following content: | 
					
						
							| 
									
										
										
										
											2015-03-02 14:46:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block run-app | 
					
						
							| 
									
										
										
										
											2015-03-02 18:42:31 -08:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     We have a few options for running our app. | 
					
						
							| 
									
										
										
										
											2015-05-29 17:55:02 -07:00
										 |  |  |     One is to launch a local HTTP server | 
					
						
							|  |  |  |     and then view the app in | 
					
						
							|  |  |  |     <a href="https://www.dartlang.org/tools/dartium/">Dartium</a>. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     We can use any web server, such as WebStorm's server | 
					
						
							| 
									
										
										
										
											2015-06-01 15:41:41 -07:00
										 |  |  |     or Python's SimpleHTTPServer. | 
					
						
							| 
									
										
										
										
											2015-03-02 18:42:31 -08:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-03-02 22:22:56 -08:00
										 |  |  |     Another option is to build and serve the app using <code>pub serve</code>, | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     and then run it by visiting <b><code>http://localhost:8080</code></b> in any modern browser. | 
					
						
							|  |  |  |     Pub serve generates JavaScript on the fly, | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     which can take a while when first visiting the page. | 
					
						
							|  |  |  |     Pub serve also runs in <b><i>watch mode</i></b>, and will recompile and subsequently serve | 
					
						
							|  |  |  |     any changed assets. | 
					
						
							| 
									
										
										
										
											2015-05-29 17:55:02 -07:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     Once the app is running, the browser window should show the following: | 
					
						
							| 
									
										
										
										
											2015-05-29 17:55:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block build-app | 
					
						
							|  |  |  |   .alert.is-important | 
					
						
							|  |  |  |     :marked | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |       If you don't see **My First Angular 2 App**, make sure you've entered all the code correctly, | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       in the [proper folders](#wrap-up), | 
					
						
							|  |  |  |       and run `pub get`. | 
					
						
							| 
									
										
										
										
											2015-06-01 09:24:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |   .l-verbose-section#section-angular-run-app | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     :marked | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |       ### Building the app (generating JavaScript) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |       Before deploying the app, we need to generate JavaScript files. | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       The `pub build` command makes that easy. | 
					
						
							| 
									
										
										
										
											2015-03-02 14:46:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 08:34:29 -07:00
										 |  |  |     code-example(language="sh" class="code-shell"). | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       > <span class="blk">pub build</span> | 
					
						
							|  |  |  |       Loading source assets... | 
					
						
							| 
									
										
										
										
											2015-10-15 23:25:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |     :marked | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       The generated JavaScript appears, along with supporting files, | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |       under a directory named `build`. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |     #angular_transformer | 
					
						
							|  |  |  |     h4 Using the Angular transformer | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     p. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |       When generating JavaScript for an Angular app, | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       be sure to use the Angular transformer. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |       It analyzes the Dart code, | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       converting reflection-using code to static code | 
					
						
							|  |  |  |       that Dart's build tools can compile to faster, smaller JavaScript. | 
					
						
							|  |  |  |       The highlighted lines in <code>pubspec.yaml</code> | 
					
						
							|  |  |  |       configure the Angular transformer: | 
					
						
							| 
									
										
										
										
											2015-05-29 17:55:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     - var stylePattern = { otl: /(transformers:)|(- angular2:)|(entry_points.*$)/gm }; | 
					
						
							|  |  |  |     +makeExample('quickstart/dart/pubspec.yaml', null, 'pubspec.yaml', stylePattern) | 
					
						
							| 
									
										
										
										
											2015-05-29 17:55:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     p. | 
					
						
							|  |  |  |       The <code>entry_points</code> item | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |       identifies the Dart file in our app | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       that has a <code>main()</code> function. | 
					
						
							|  |  |  |       For more information, see the | 
					
						
							|  |  |  |       <a href="https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer">Angular | 
					
						
							|  |  |  |       transformer wiki page</a>. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |     .l-sub-section#performance | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |       h3 Performance, the transformer, and Angular 2 libraries | 
					
						
							|  |  |  |       p. | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |         When an app imports <code>bootstrap.dart</code>, | 
					
						
							|  |  |  |         it also gets <code>dart:mirrors</code>, | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |         a reflection library that | 
					
						
							|  |  |  |         causes performance problems when compiled to JavaScript. | 
					
						
							|  |  |  |         Don't worry, | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |         the Angular transformer converts the app's entry points | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |         (<code>entry_points</code> in <code>pubspec.yaml</code>) | 
					
						
							|  |  |  |         so that they don't use mirrors. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:32:07 -07:00
										 |  |  |     #dart_to_js_script_rewriter | 
					
						
							|  |  |  |     h4 Using dart_to_js_script_rewriter | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     :marked | 
					
						
							|  |  |  |       To improve the app's performance, convert the | 
					
						
							|  |  |  |       HTML file to directly include the generated JavaScript; | 
					
						
							|  |  |  |       one way to do that is with `dart_to_js_script_rewriter`. | 
					
						
							|  |  |  |       To use the rewriter, specify `dart_to_js_script_rewriter` in both | 
					
						
							|  |  |  |       the `dependencies` and `transformers` sections of the pubspec. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - var stylePattern = { otl: /(dart_to_js_script_rewriter.*$)|(- dart_to_js_script_rewriter.*$)|(dependencies:)|(transformers:)/gm }; | 
					
						
							|  |  |  |     +makeExample('quickstart/dart/pubspec.yaml', null, 'pubspec.yaml', stylePattern) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .alert.is-important | 
					
						
							|  |  |  |       :marked | 
					
						
							|  |  |  |         The `dart_to_js_script_rewriter` transformer must be | 
					
						
							|  |  |  |         **after** the `angular2` transformer in `pubspec.yaml`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :marked | 
					
						
							|  |  |  |       For more information, see the docs for | 
					
						
							|  |  |  |       [dart_to_js_script_rewriter](https://pub.dartlang.org/packages/dart_to_js_script_rewriter). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block server-watching | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     To see the new version, just reload the page. | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |   .alert.is-important | 
					
						
							|  |  |  |     :marked | 
					
						
							|  |  |  |       Be sure to terminate the `pub serve` process once you stop working on this app. | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | block project-file-structure | 
					
						
							|  |  |  |   .filetree | 
					
						
							| 
									
										
										
										
											2016-07-01 11:05:45 -07:00
										 |  |  |     .file angular2_quickstart | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     .children | 
					
						
							|  |  |  |       .file lib | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file app_component.dart | 
					
						
							|  |  |  |       .file pubspec.yaml | 
					
						
							|  |  |  |       .file web | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file index.html | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |         .file main.dart | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |         .file styles.css | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |   .l-verbose-section | 
					
						
							|  |  |  |     :marked | 
					
						
							|  |  |  |       This figure doesn't show generated files and directories. | 
					
						
							|  |  |  |       For example, a `pubspec.lock` file | 
					
						
							|  |  |  |       specifies versions and other identifying information for | 
					
						
							|  |  |  |       the packages that our app depends on. | 
					
						
							|  |  |  |       The `pub build` command creates a `build` directory | 
					
						
							|  |  |  |       containing the JavaScript version of our app. | 
					
						
							|  |  |  |       Pub, IDEs, and other tools often create | 
					
						
							|  |  |  |       other directories and dotfiles. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  | block project-files | 
					
						
							|  |  |  |   +makeTabs(` | 
					
						
							|  |  |  |       quickstart/ts/app/app.component.ts, | 
					
						
							|  |  |  |       quickstart/ts/app/main.ts, | 
					
						
							|  |  |  |       quickstart/ts/index.html, | 
					
						
							|  |  |  |       quickstart/dart/pubspec.yaml, | 
					
						
							| 
									
										
										
										
											2016-08-17 09:21:11 -07:00
										 |  |  |       quickstart/ts/styles.css`, | 
					
						
							|  |  |  |     ',,,,quickstart', | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     `app/app.component.ts, | 
					
						
							|  |  |  |     app/main.ts, | 
					
						
							| 
									
										
										
										
											2016-05-14 17:40:52 -07:00
										 |  |  |     index.html, | 
					
						
							| 
									
										
										
										
											2016-05-16 17:10:19 -07:00
										 |  |  |     pubspec.yaml, | 
					
						
							| 
									
										
										
										
											2016-08-17 09:21:11 -07:00
										 |  |  |     styles.css (excerpt)`) |