Angular 5 – Routing (Practical Guide)

Angular provides an easy way to create and work with components , In a single page application(SPA) it is essential to work with multiple views/screens, navigate and communicate between them. Angular provides router service to manage this in a very easy way.

We can create a routing module while creating a new application or add it later

To create a new Angular application with routing support:

Assume we want to display a menu bar with the following titles:

First we will add bootstrap support to our app – see this page for instructions

Add 4 components (books, customers,loans, welcome). Add component for Page Not Found

Add bootstrap Navbar to the app.component.html file , also add <router-outlet> directive to specify the place holder for the routed components:

Now create the routing table in app.routing.ts file:

Each entry specify the address bar path and the component to render into the router-outlet

the path ‘**’ specify any non valid route and navigate to PageNotFound component

Add the routing object to the module imports (app.module.ts)

Adding parameters

You can add parameters to the route to send extra info – for example if you want Master-Details view:

While clicking details we want to navigate to a page with full book details.

Add a new component: BookDetails

Update the routing table (Add new object):

The parameter name is “id” , and the link should look like

You can add more parameters this way for example to add the price: ¬†path: ‘book/:id/:price’

To add a link for book details use [routerLink] property binding. For example assume we have a list of Books objects we can create the table with the links:

Navigate programatically

You can change the routing link to any event and use the router service to navigate:

When you navigate you can add query parameters:

And fragment

On the BookDetails component you can read the parameters using ActivatedRoute service:

The ActivatedRoute Service

The ActivatedRoute service lets you read the routing parameters, the query parameters and the fragment name

Another useful feature is to  subscribe to changes in query params or fragments:

Creating Child routes

Sometimes we want a child route for example we want another menu in Customer component with some titles: edit, new, details, etc.

You need to create the components and update the route table:

Also, Add <router-outlet> directive to the parent component (customers)

Navigation Guards

For better UX we need to disable a route sometimes (for example the customer didn’t fill the form yet). We can write code to guard the navigation:

To create a guard we can use Angular CLI:

We need to write a class and implement CanActivate, CanDeactivate, and more (see docs)

For example we navigate to book only if the price is greater than 200 – assume the navigation has 2 parameters ‘book/:id/:price’

we need to add the class to the routing table:

And because its a service we need to add it to the providers array of the application module (app.module.ts)

We can also run the guard code asynchronously – the canActivate function returns boolean or Promise or Observable:






Leave a Reply

Your email address will not be published.