16 Using Angular components and routing

 

This chapter covers

  • Creating Angular components
  • Navigating between components with routing

In the last chapter, you learned how to create a NativeScript-with-Angular app and began to re-create the Pet Scrapbook. We’ll be continuing to refine the Pet Scrapbook Angular by learning about Angular components and routing, two core concepts that parallel the concepts of vanilla NativeScript: pages and navigation.

In our experience, learning about Angular concepts on their own (without the context of a vanilla NativeScript app) can be confusing, so we’ve taken the time to build on things you’ve already learned about to help you quickly learn about Angular and how it’s used in a NativeScript-with-Angular app. Just like the last chapter, if you haven’t walked-through the official QuickStart guide for Angular, we strongly recommend you check it out at https://angular.io/docs/ts/latest/quickstart.html.

16.1   Creating static components

As you’ll recall, you learned that the app.module.ts file is the entry point for NativeScript-with-Angular apps. You also learned that Angular components are like NativeScript pages, but let’s dive a little deeper into how components and pages are similar. Figure 16.1 shows the contents of an Angular component.

Figure 16.1 Angular components have markup, style, and code.

16.1.1   Home component

16.1.2   Loading the Home component

16.2   Navigating between components with routing

16.2.1   Creating the List component

16.2.2   Page navigation in NativeScript-with-Angular

16.3   Summary

16.4   Exercise

16.5   Solutions

16.5.1   Step 1: Create the about component files

16.5.2   Step 2: Add UI markup to the about component

16.5.3   Step 3: Define the about component code

16.5.4   Step 4: Import the about component into the app module

16.5.5   Step 5: Add the about component route to the app module

16.5.6   Step 6: In the Home component’s UI markup, add a tap event to the About button

16.5.7   Step 7: Handle the About button’s tap event, navigating to the about route

sitemap