Angular Routing is a default feature of the application. In this tutorial we will learn about Angular routing.
Introducing Angular Router
Angular Router is a powerful JavaScript router built and maintained by the Angular core team that can be installed from the @angular/router
package. It provides a complete routing library with the possibility to have multiple router outlets, different path matching strategies, easy access to route parameters and route guards to protect components from unauthorized access.
The Angular router is a core part of the Angular platform. It enables developers to build Single Page Applications with multiple views and allow navigation between these views.
Step 1 – Create Routing File
Our first setup is to create routing file named “app-routing.module.ts”, where you will put all your route.
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = []; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
Step 2 – Import routing module
Next, We will import above created file in the ‘app.module.ts’ file
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Step 3 – Create two components
Now we will create two component. One component for one route. run below command to create components
ng g component firstPage ng g component secondPage
Step 4 – Create route and attach the component
Next, We will create two route and attach component on it. So when user navigate the route he / she will see the separate component. Open “app-routing.module.ts” file.
Import our components
import { FirstPageComponent } from './first-page/first-page.component'; import { SecondPageComponent } from './second-page/second-page.component';
Next, Create route like this
const routes: Routes = [ { path: 'first-page', component: FirstPageComponent }, { path: 'second-page', component: SecondPageComponent } ];
After update our file will look like this
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { FirstPageComponent } from './first-page/first-page.component'; import { SecondPageComponent } from './second-page/second-page.component'; const routes: Routes = [ { path: 'first-page', component: FirstPageComponent }, { path: 'second-page', component: SecondPageComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AdminRoutingModule { }
Step 5 – Add router-outlet
Now, we need the above component to be displayed whenever required or clicked from the main module. For this, we need to add the router details in the app.component.html.
<br/> <a routerLink = "first-page">First Page</a> | <a routerLink = "second-page">Second Page</a> <br /> <br/> <router-outlet></router-outlet>
In the above code, we have created the anchor link tag and given routerLink as “first-page” and “second-page” . This is referred in app-routing.module.ts as the path.
When a user clicks on “first-page” or “second-page” , the page should display the content. For this, we need the following tag – <router-outlet> </router-outlet>.
The above tag ensures that the content in the first-page.component.html or second-page.component.html will be displayed on the page when a user clicks on “first-page” or “second-page”
Let see how the output is displayed on the browser.

Pretty! This has been an incredibly wonderful article. Thanks for supplying this info. Clary Nicolas Debor
Good post! We will be linking to this great post on our website. Keep up the good writing. Beverlee Hayward Anderegg
Hi there friends, its impressive piece of writing regarding educationand fully explained, keep it up all the time. Carol-Jean Branden Alessandra
This post will help the internet viewers for setting up new blog or even a blog from start to end. Evita Caryl Andel
I think that is one of the such a lot vital info for me. Abbey Davidde Kamin
Very neat blog article. Much thanks again. Awesome. Faustine Cori Sergent
hello!,I love your writing very much! proportion we communicate more approximately your article on AOL? I require an expert in this space to unravel my problem. Maybe that is you! Taking a look ahead to peer you. Moina Dimitri Vail
Hey, I think your website might be having browser compatibility issues. When I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, awesome blog!| Tilly Dennis Gabby
Hallo, Ich finde den Aufbau der Seite super. Macht weiter so. Kayley Derrek Aekerly
WOW just what I was looking for. Came here by searching for transfertvtc74.com Catriona Javier Berton
Hello my friends, how is everything? Here it is truly good YouTube video tutorials collection. i enjoyed a lot. Loni Sigmund Kotta
Very nice write-up. I certainly love this site. Thanks! Ansley Cullie Lash
Thank you! Yes, Markus Zusak is such an interesting writer. He really tells the story with so much foreshadowing. Dixie Willem Kenway
Some really quality blog posts on this website , saved to my bookmarks . Rosalinda Burl Mateusz
How can u add a flash slide show to your blogspot? Gilberte Zane Ordway
I want to get across my gratitude for your generosity for people that require help on this important area of interest. Your very own commitment to getting the solution all through has been definitely powerful and has surely helped guys like me to realize their goals. The informative information means this much to me and far more to my colleagues. Many thanks; from each one of us. Gerladina Ethan Matusow
Hi! I just wanted to ask if you ever have any issues with hackers? Kate Edan Amil
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?| Hynda Farlay Carlin
Very interesting points you have noted, regards for putting up. Natividad Even Juni
Nullam at leo tincidunt felis facilisis tincidunt. Nunc sit amet velit metus. Proin posuere, ligula nec porttitor eget luctus, risus lectus tristique ligula, quis pretium elit diam a nisi. Proin vehicula malesuada dolor, vel rutrum. Ludovika Ced Modestine
Looking forward to reading more. Great blog. Thanks Again. Fantastic. Tiffy Portie Constantia
The root of your writing while sounding reasonable at first, did not really sit well with me after some time. Someplace within the paragraphs you actually managed to make me a believer but just for a short while. I however have a problem with your leaps in logic and you might do nicely to fill in those breaks. When you actually can accomplish that, I would surely be impressed. Grayce Roderick Roti
Excellent post. Keep posting such kind of information on your site. Maritsa Bartholemy Hoskinson
Hi my name is Alison and I am a tots a biggest fan I love all of your song. Marissa Torey Delanty
Muchos Gracias for your blog article. Thanks Again. Great. Sue Torrin Hungarian
Everyone loves it when individuals come together and share views. Great site, keep it up! Dedra Karlis Deni
Pretty! This has been an extremely wonderful article. Many thanks for providing this info.| Patti Lothaire Wurst
Thanks for sharing such a nice thought, post is pleasant, thats why i have read it fully Perry Tyler Service
Very nice write-up. I definitely appreciate this website. Continue the good work! Netty Dwain Severson
Wow! After all I got a weblog from where I be able to actually obtain useful facts regarding my study and knowledge. Alexa Hilarius Keheley
you will have an excellent weblog here! would you like to make some invite posts on my blog? Blithe Shannon Floria
An intriguing discussion is definitely worth comment. I believe that you need to publish more about this topic, it might not be a taboo subject but usually people do not talk about such topics. To the next! Many thanks. Ernestine Prinz Kessia
Wow! Thank you! I permanently needed to write on my site something like that. Can I take a fragment of your post to my website? Livy Nathanael Pedersen
Appreciate it for all your efforts that you have put in this. very interesting info . Eloisa Walker Yelich
Today, while I was at work, my sister stole my apple ipad and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!| Helaine Eli Glialentn
Welcome to iBeautys Best, iBeautys Best is the number one destination for Latest Beauty Products. Here you can easily buy your desired product from all over the world with very reasonable price. Harriot Julie Nikaniki
At this time it seems like Expression Engine is the top blogging platform available right now. Barbe Devy Fitton
Hey There. I found your weblog the usage of msn. This is an extremely well written article. Kaile Yardley Lowndes
Thanks for always keeping your WordPress themes up to date. Your level of support and dedication is second to none. Catlin North Purvis
Your style is unique in comparison to other folks I have read stuff from. Tally Kane Janis
I cling on to listening to the newscast talk about getting free online grant applications so I have been looking around for the top site to get one. Could you advise me please, where could i get some? Gates Gabie Pestana
I used to be abloe to find good information from your blog posts. Isabelle Ange Grodin
Skippyjon Jones Shape Up By Judy Schachner Unlimited Power
Keyboard
My web page – wordpress.org
I am sure this piece of writing has touched all the internet users, its really really good post on building up new blog.| Jaimie Rockie Gerrie
Soccer video games are a great way to help you learn the game better. Violante Lennie Rasla
Good response in return of this query with real arguments and telling everything concerning that. Arlene Weber Vivle
You should be part of a competition for starters from the greatest blogs on the internet.
I’m likely to recommend this amazing site!
Check out my web blog … SheilaEByner