Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MEAN Cookbook

You're reading from   MEAN Cookbook The meanest set of MEAN stack solutions around

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781787286573
Length 450 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Nicholas McClay Nicholas McClay
Author Profile Icon Nicholas McClay
Nicholas McClay
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Working with Angular 4 FREE CHAPTER 2. Enhancing Your User Interface 3. Working with Data 4. Using Express Web Server 5. REST APIs and Authentication 6. Cloud Service Integrations 7. MongoDB and Mongoose 8. Relationships 9. Build Systems and Optimizations 10. Debugging 11. Automated Testing 12. Whats new in Angular 4

Generating new routes in Angular-CLI

Now we are ready to start really digging into the details of routing in Angular; how to create new routes and how to redirect to routes in your application, including error handling. We'll also cover how to nest routes and use route preloading to speed up your application navigation.


Creating new routes
: Routing in Angular is handled by the optional Angular router package and must be imported into your application to be used.

Getting ready

To mount the router to the root of our application, we must first import the RouterModule from the router and call the forRoot method to tell the router that we are setting up routes on the root of our web application:

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component'

@NgModule({
...
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot([])
],
...
})
export class AppModule { }

With the router set up on the root of our web application, we are now able to provide a mapping between the Universal Resource Indentifier (URI) of the route that we want to show in the browser and the component we want to be rendered on that route. For the sake of simplicity, we will imagine we are building a simple blog website and will stub out two components: posts and authors. Posts will show all the available blog posts on our blog, whereas authors will show a list of all authors of blog posts on our blog:

ng g component posts
ng g component authors

This will scaffold out the two components as well as add them to our app module so that we can start routing to them.

How to do it...

After scaffolding the two components and adding them to our app module, let’s follow these steps to add our routes:

  1. First, we will need to create the route map between these components and the two routes we want to create: /posts and /authors. We will do this by passing an array of route configuration objects into the forRoot method of our RouterModule. This configuration tells the router that the /posts route will resolve with the PostsComponent, and the /authors route will resolve with the AuthorsComponent:
@NgModule({
...
imports: [
...
RouterModule.forRoot([
{
path: "posts",
component: PostsComponent

},
{

path: "authors",
component: AuthorsComponent

}
])
],
...
})
export class AppModule { }
  1. With our routes and components now properly associated, we just need to provide an outlet, where this content can be presented, and links to make navigating to it more easily. We can add both of these to our app.component.html template, as follows:
<nav>
<a routerLink="/posts">Posts</a>
<a routerLink="/authors">Authors</a>
</nav>
<router-outlet></router-outlet>

How its works...

Instead of a traditional href attribute, we tell the router to navigate to a route we've configured by attaching the routerLink directive to it. This directive takes a path string that matches a path configured with our router. Next, we provide the outlet where our content will be presented through when a route is active with the router-outlet directive. With these two elements in place, we can see two folders called Posts and Authors in our app folder; clicking on either link will add the posts works! text for /posts and authors works! for /authors.

By updating the template within our components, we can change the content that shows up when the user navigates to the associated route. We can also change the template in app.component.html to alter the shared master template that is used by both routes. We will explore this topic more in the next chapter when we upgrade our application's styling.

You have been reading a chapter from
MEAN Cookbook
Published in: Sep 2017
Publisher: Packt
ISBN-13: 9781787286573
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image