Angular routing, A Complete Guide To Routing In Angular

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.

Recommended Posts

Angular Guard

47 Comments

  1. online said:

    Pretty! This has been an incredibly wonderful article. Thanks for supplying this info. Clary Nicolas Debor

    December 10, 2020
    Reply
  2. filmi full izle said:

    Good post! We will be linking to this great post on our website. Keep up the good writing. Beverlee Hayward Anderegg

    January 17, 2021
    Reply
  3. filmi izle said:

    Hi there friends, its impressive piece of writing regarding educationand fully explained, keep it up all the time. Carol-Jean Branden Alessandra

    January 18, 2021
    Reply
  4. filmi izle said:

    This post will help the internet viewers for setting up new blog or even a blog from start to end. Evita Caryl Andel

    January 18, 2021
    Reply
  5. free porn said:

    I think that is one of the such a lot vital info for me. Abbey Davidde Kamin

    January 21, 2021
    Reply
  6. dublaj said:

    Very neat blog article. Much thanks again. Awesome. Faustine Cori Sergent

    January 29, 2021
    Reply
  7. dublaj said:

    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

    January 30, 2021
    Reply
  8. watch said:

    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

    January 30, 2021
    Reply
  9. dublaj said:

    Hallo, Ich finde den Aufbau der Seite super. Macht weiter so. Kayley Derrek Aekerly

    January 30, 2021
    Reply
  10. dublaj said:

    WOW just what I was looking for. Came here by searching for transfertvtc74.com Catriona Javier Berton

    January 31, 2021
    Reply
  11. bollywood said:

    Hello my friends, how is everything? Here it is truly good YouTube video tutorials collection. i enjoyed a lot. Loni Sigmund Kotta

    January 31, 2021
    Reply
  12. altyazili said:

    Very nice write-up. I certainly love this site. Thanks! Ansley Cullie Lash

    February 1, 2021
    Reply
  13. filmkovasi said:

    Thank you! Yes, Markus Zusak is such an interesting writer. He really tells the story with so much foreshadowing. Dixie Willem Kenway

    February 4, 2021
    Reply
  14. bedava said:

    Some really quality blog posts on this website , saved to my bookmarks . Rosalinda Burl Mateusz

    February 8, 2021
    Reply
  15. yabanci said:

    How can u add a flash slide show to your blogspot? Gilberte Zane Ordway

    February 8, 2021
    Reply
  16. diziler said:

    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

    February 9, 2021
    Reply
  17. 720p said:

    Hi! I just wanted to ask if you ever have any issues with hackers? Kate Edan Amil

    February 10, 2021
    Reply
  18. turkce said:

    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

    February 13, 2021
    Reply
  19. turkce said:

    Very interesting points you have noted, regards for putting up. Natividad Even Juni

    February 13, 2021
    Reply
  20. turkce said:

    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

    February 13, 2021
    Reply
  21. turkce said:

    Looking forward to reading more. Great blog. Thanks Again. Fantastic. Tiffy Portie Constantia

    February 13, 2021
    Reply
  22. turkce said:

    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

    February 13, 2021
    Reply
  23. turkce said:

    Excellent post. Keep posting such kind of information on your site. Maritsa Bartholemy Hoskinson

    February 13, 2021
    Reply
  24. turkce said:

    Hi my name is Alison and I am a tots a biggest fan I love all of your song. Marissa Torey Delanty

    February 14, 2021
    Reply
  25. turkce said:

    Muchos Gracias for your blog article. Thanks Again. Great. Sue Torrin Hungarian

    February 14, 2021
    Reply
  26. turkce said:

    Everyone loves it when individuals come together and share views. Great site, keep it up! Dedra Karlis Deni

    February 14, 2021
    Reply
  27. turkce said:

    Pretty! This has been an extremely wonderful article. Many thanks for providing this info.| Patti Lothaire Wurst

    February 14, 2021
    Reply
  28. turkce said:

    Thanks for sharing such a nice thought, post is pleasant, thats why i have read it fully Perry Tyler Service

    February 14, 2021
    Reply
  29. turkce said:

    Very nice write-up. I definitely appreciate this website. Continue the good work! Netty Dwain Severson

    February 14, 2021
    Reply
  30. turkce said:

    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

    February 14, 2021
    Reply
  31. turkce said:

    you will have an excellent weblog here! would you like to make some invite posts on my blog? Blithe Shannon Floria

    February 14, 2021
    Reply
  32. turkce said:

    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

    February 15, 2021
    Reply
  33. turkce said:

    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

    February 15, 2021
    Reply
  34. turkce said:

    Appreciate it for all your efforts that you have put in this. very interesting info . Eloisa Walker Yelich

    February 15, 2021
    Reply
  35. turkce said:

    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

    February 15, 2021
    Reply
  36. turkce said:

    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

    February 15, 2021
    Reply
  37. turkce said:

    At this time it seems like Expression Engine is the top blogging platform available right now. Barbe Devy Fitton

    February 15, 2021
    Reply
  38. turkce said:

    Hey There. I found your weblog the usage of msn. This is an extremely well written article. Kaile Yardley Lowndes

    February 15, 2021
    Reply
  39. turkce said:

    Thanks for always keeping your WordPress themes up to date. Your level of support and dedication is second to none. Catlin North Purvis

    February 15, 2021
    Reply
  40. erotik said:

    Your style is unique in comparison to other folks I have read stuff from. Tally Kane Janis

    February 16, 2021
    Reply
  41. erotik said:

    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

    February 16, 2021
    Reply
  42. erotik said:

    I used to be abloe to find good information from your blog posts. Isabelle Ange Grodin

    February 16, 2021
    Reply
  43. Jamila said:

    Skippyjon Jones Shape Up By Judy Schachner Unlimited Power
    Keyboard

    My web page – wordpress.org

    February 16, 2021
    Reply
  44. turkce said:

    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

    February 16, 2021
    Reply
  45. 샌즈카지노 said:

    Soccer video games are a great way to help you learn the game better. Violante Lennie Rasla

    February 17, 2021
    Reply
  46. 샌즈카지노 said:

    Good response in return of this query with real arguments and telling everything concerning that. Arlene Weber Vivle

    February 17, 2021
    Reply
  47. Jayson said:

    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

    February 22, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *