Nested Routing In A NativeScript Angular Application For Android And iOS

When building mobile applications you’ll often find yourself needing to create child components. What do I mean by child components? Take for example a mobile application with two different screen groupings, one where the user is not signed in and the other where the user is signed in. In each of these groupings you could have multiple screens where the user is signed in and multiple screens where the user is not signed in. Each screen under the parent grouping can be considered a child component and they could possibly share a template from the parent. These child components often require nested routing to occur for a successful navigation.

We’re going to see how to create nested routes in a NativeScript mobile application built with Angular. You’ll see that things aren’t much different from creating standard routes in an Angular application.

The application we build will be very simple. We’ll have two parent components where one of the parent components has two child components. The other parent will have no child components. If you remember my work from AngularJS, you’ll remember I did something similar with nested states and the UI-Router. AngularJS is now ancient technology so we need to see how to do this with the latest Angular.

For simplicity, we’ll create a fresh project and work from that. With NativeScript installed, from the Command Prompt (Windows) or Terminal (Mac and Linux), execute the following:

tns create NestedProject --ng

In later versions of NativeScript, we no longer need to add build platforms for Android and iOS. This is done automatically at build and run time.

With the base project created we can add a few components. If you’re using a Mac or Linux computer, execute the following from the command line:

If you’re on Windows, you can create each of the above files and directories manually.

Before we start creating each of the application screens, we should probably define them as routes and bootstrap them in the application. This needs to be done in two different files, but can vary depending on your project setup or version. Open the project’s app/app.routing.ts file and include the following code:

What is important in the above are the route definitions. Two parent routes are defined, one with a default path (the empty one), and one that doesn’t act as the default path. The default route has two children, one of which is the default child. Just like with the parent, the default child is the one with the empty path. This rule applies for any routing in Angular.

Default routes can be represented by the slash character. Navigating to the default route will actually navigate to the default child in this scenario. The other parent route is not a default route so it has no leading slash.

With those routes defined, we need to make a few changes to the main @NgModules block of our application. Open the project’s app/app.module.ts file and include the following code:

Being that the ParentComponent and ChildComponent classes are our default pages, we should probably set them up, starting with the parent. Open the project’s app/components/parent/parent.component.ts file and include the following TypeScript code:

Nothing special happening in the above, so let’s look at the HTML file that is paired with it. Open the project’s app/components/parent/parent.component.html file and include the following HTML markup:

This is a pretty standard layout, but what is special here is the use of the <router-outlet> tags. These are not the same as the <page-router-outlet> tags that were seen previously. The <router-outlet> tags act as a pass-through for any child routes. This means that when we load our application, we’ll see the navigation bar, the parent component text, and whatever is in the child component.

So let’s look at that first child component.

Open the project’s app/components/child/child.component.ts file and include the following TypeScript code:

In the above we have two methods that will be used for navigation. One will navigate to the other child component, and one that will navigate to the other parent component which has no children. Remember what the route paths mean that were described earlier. With the TypeScript out of the way, open the project’s app/components/child/child.component.html file and include the following HTML markup:

In this child component we have text as well as two buttons. Each button will call the appropriate function for navigation. Think back to the animated image seen at the beginning of the tutorial. On the default screen you’ll see the navigation bar, the parent text, the child text, and the buttons for navigation. This is through nested routing.

So what does the other child component look like? Nothing special.

Open the project’s app/components/otherchild/otherchild.component.ts file and include the following TypeScript code:

For this example there is no special logic for the above TypeScript file. The HTML file that follows is nothing special either, but within the project’s app/components/otherparent/otherparent.component.html file you should have the following:

At this point you should have a multiple route application that includes several child routes that share a parent template.

Conclusion

You just saw how to use nested routing to create parent and child pages for your NativeScript with Angular application. This is similar to my previous AngularJS article on the topic, but this time we’re using the latest Angular. To put things into context, my application, Solar Flare, uses nested routing for the authorized and unauthorized screens. The authorized screens share a parent route which includes a navigation drawer.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.