You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>This file contains an Angular 2 component, which is the primary building block of Angular 2 applications, including NativeScript apps. Let’s break down what’s going on in this file.</p>
307
307
<p>First, you again use TypeScript’s <code>import</code> command to bring in externally defined functionality—in this case, the <code>Component</code> class from Angular 2 itself. In Angular 2 a component manages a view, or a piece of the user interface that the user sees. A component can be used to define an individual UI element, or an entire page, and eventually we’ll add a bunch of logic to these components and use them to build an entire app. But for now this component is simple for the purpose of demonstration.</p>
308
+
<blockquote>
309
+
<p><strong>TIP</strong>: Why <ahref="http://www.typescriptlang.org/">TypeScript</a>? It's strongly recommended that you use TypeScript in your Angular 2 NativeScript app, as it's a first class citizen in both NativeScript and Angular 2. In fact, both NativeScript and Angular 2 were built with TypeScript. The NativeScript CLI makes compiling your TypeScript files seamless, as each time you livesync, run or build the app, the files are recompiled from TypeScript to JavaScript. In some IDEs, such as Visual Studio Code, you might choose to hide the compiled files. A tutorial on how to do this can be found <ahref="https://code.visualstudio.com/Docs/languages/typescript#_hiding-derived-javascript-files">here</a>.</p>
310
+
</blockquote>
308
311
<p>Notice the interesting way that the <code>Component</code> class is used—with the syntax <code>@Component</code>. This is a <ahref="https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Decorators.md">TypeScript decorator</a>, which allows you to annotate a TypeScript class or method with additional information. For now, you can think of it as a way of adding some metadata configuration to the currently empty <code>AppComponent</code> class. Specifically, the <code>@Component</code> decorator’s <code>template</code> property tells NativeScript how to render this component on the screen. In fact, the <code><Label text="hello NativeScript"></Label></code> syntax is why you saw “hello NativeScript” when you ran this app earlier.</p>
309
312
<p>However, this syntax may look a bit odd if you come from a web development background. On the web, the <code><label></code> HTML element doesn’t have a <code>text</code> attribute, so why do we see it here? Let’s dive into this by looking at how NativeScript UI elements work.</p>
310
313
<blockquote>
@@ -906,16 +909,20 @@ <h4 class="exercise-start">
906
909
</blockquote>
907
910
<p>Your app now has a fully functional registration process, but users can’t do anything with the accounts they create. Our next step is to allow the users to login and navigate to a new list page. And to do that we need to introduce the concept of routing.</p>
908
911
<h3id="routing">Routing</h3>
909
-
<p>TODO: Intro</p>
912
+
<p>Most of the Angular 2 concepts you’ve used in the last few sections are the same regardless of whether you’re building for the web or native apps. You are even able to share model objects and services directly between the two environments.</p>
913
+
<p>However, routing is different, as there are some fundamental differences between how the concept of routing works on the web and in apps. Routing on the web revolves around the concept of a URL, but in a native app there is no browser. Likewise, native apps have concepts that aren’t present in the browser, such as Android’s hardware back button, or iOS’s swipe left-to-right gesture to go back.</p>
914
+
<p>Because of that, routing in NativeScript needs to work a bit differently. The main difference is that you must designate certain Angular components in your app as “pages”, which in Groceries are stored in the <code>pages</code> folder. Let’s look at the code needed to make this work, and then step by and discuss why NativeScript takes the approach that it does.</p>
910
915
<h4class="exercise-start">
911
-
<b>Exercise</b>: ???
916
+
<b>Exercise</b>: Setting up routing
912
917
</h4>
913
918
914
-
<p>Copy app/app.component.ts into app/pages/login/login.component.ts, change the name of the class from “AppComponent” to “LoginPage, and update the two paths below accordingly:</p>
919
+
<p>To this point you’ve been putting your login page code in <code>app.component.ts</code>. Let’s move that logic into the <code>pages/login</code> folder to make room for additional pages.</p>
920
+
<p>First, open <code>app/app.component.ts</code> and copy its contents into <code>app/pages/login/login.component.ts</code>.</p>
921
+
<p>Next, in <code>login.component.ts</code>, change the name of the class from “AppComponent” to “LoginPage, and update the two paths below accordingly:</p>
915
922
<pre><codeclass="lang-TypeScript">import {User} from "../../shared/user/user";
916
923
import {UserService} from "../../shared/user/user.service";
917
924
</code></pre>
918
-
<p>Open app/app.component.ts back up and paste in the following code:</p>
925
+
<p>Now that <code>app.component.ts</code> is empty, let’s add in the appropriate Angular 2 routing code. Open app/app.component.ts back up and paste in the following code:</p>
919
926
<pre><codeclass="lang-TypeScript">import {Component} from "angular2/core";
920
927
import {HTTP_PROVIDERS} from "angular2/http";
921
928
import {RouteConfig} from "angular2/router";
@@ -933,7 +940,6 @@ <h4 class="exercise-start">
933
940
])
934
941
export class AppComponent {}
935
942
</code></pre>
936
-
<p>Explain what’s going on here.</p>
937
943
<divclass="exercise-end"></div>
938
944
939
945
<p>Let’s actually log the user in then, shall we?</p>
@@ -996,7 +1002,9 @@ <h4 class="exercise-start">
996
1002
</code></pre>
997
1003
<divclass="exercise-end"></div>
998
1004
999
-
<p>You can login now! And navigate! Show a gif of the navigation. Talk about how in NativeScript you get native behavior automatically—aka a back button on iOS and a hardware back button on Android.</p>
1005
+
<p>You can login now! And navigate! </p>
1006
+
<p><imgalt="Login with basic information" src="images/chapter3/login_router.gif"></p>
1007
+
<p>Talk about how in NativeScript you get native behavior automatically—aka a back button on iOS and a hardware back button on Android.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+13-7Lines changed: 13 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -481,20 +481,28 @@ Your app now has a fully functional registration process, but users can’t do a
481
481
482
482
### Routing
483
483
484
-
TODO: Intro
484
+
Most of the Angular 2 concepts you’ve used in the last few sections are the same regardless of whether you’re building for the web or native apps. You are even able to share model objects and services directly between the two environments.
485
+
486
+
However, routing is different, as there are some fundamental differences between how the concept of routing works on the web and in apps. Routing on the web revolves around the concept of a URL, but in a native app there is no browser. Likewise, native apps have concepts that aren’t present in the browser, such as Android’s hardware back button, or iOS’s swipe left-to-right gesture to go back.
487
+
488
+
Because of that, routing in NativeScript needs to work a bit differently. The main difference is that you must designate certain Angular components in your app as “pages”, which in Groceries are stored in the `pages` folder. Let’s look at the code needed to make this work, and then step by and discuss why NativeScript takes the approach that it does.
485
489
486
490
<h4class="exercise-start">
487
-
<b>Exercise</b>: ???
491
+
<b>Exercise</b>: Setting up routing
488
492
</h4>
489
493
490
-
Copy app/app.component.ts into app/pages/login/login.component.ts, change the name of the class from “AppComponent” to “LoginPage, and update the two paths below accordingly:
494
+
To this point you’ve been putting your login page code in `app.component.ts`. Let’s move that logic into the `pages/login` folder to make room for additional pages.
495
+
496
+
First, open `app/app.component.ts` and copy its contents into `app/pages/login/login.component.ts`.
497
+
498
+
Next, in `login.component.ts`, change the name of the class from “AppComponent” to “LoginPage, and update the two paths below accordingly:
Open app/app.component.ts back up and paste in the following code:
505
+
Now that `app.component.ts` is empty, let’s add in the appropriate Angular 2 routing code. Open app/app.component.ts back up and paste in the following code:
498
506
499
507
```TypeScript
500
508
import {Component} from"angular2/core";
@@ -515,8 +523,6 @@ import {LoginPage} from "./pages/login/login.component";
0 commit comments