How to enable routing and navigation between component pages in Angular 8 ?
The task is to enable routing between angular components by making their routes when a user clicks the link, it will be navigated to page link corresponding to the required component.
Let us know what is routing in Angular
Angular 8 routing:
The Angular 8 Router helps to navigate between pages that are being triggered by the user’s actions. The navigation happens when the user clicks on the link or enter the URL from the browser address bar. The link can contain the reference to the router on which the user will be directed. We can also pass other parameters with a link through angular routing.
Approach:
- Create an Angular app.
syntax:
ng new app_name
- For routing you will need components, here we have made two components (home and dash) to display home page and dashboard.
syntax:
ng g c component_name
- In app.module.ts, import RouterModule from @angular/router.
syntax:
import { RouterModule } from '@angular/router'; - Then in imports of app.module.ts define the paths.
imports: [ BrowserModule, AppRoutingModule, RouterModule.forRoot([ { path: 'home', component: HomeComponent }, { path: 'dash', component:DashComponent } ]) ], - Now for HTML part, define the HTML for app.component.html. In link, define routerLink’s path as the component name.
<a routerLink="/home">Home </a><br> <a routerLink="/dash">dashboard</a>
- Apply router-outlet for your application in app.component.html.The routed views render in the <router-outlet>
<router-outlet></router-outlet>
- Now just define HTML for home.component.html and dash.component.html file.
- Your angular web-app is ready.
Code Implementation:
-
app.module.ts:
import { BrowserModule } from'@angular/platform-browser';import { NgModule } from'@angular/core';import { RouterModule } from'@angular/router';import { AppRoutingModule } from'./app-routing.module';import { AppComponent } from'./app.component';import { HomeComponent } from'./home/home.component';import { DashComponent } from'./dash/dash.component';@NgModule({declarations: [AppComponent,HomeComponent,DashComponent],imports: [BrowserModule,AppRoutingModule,RouterModule.forRoot([{ path:'home', component: HomeComponent },{ path:'dash', component:DashComponent }])],providers: [],bootstrap: [AppComponent]})export class AppModule { }chevron_rightfilter_none -
app.component.html
<arouterLink="/home">Home </a><br><arouterLink="/dash">dashboard</a><router-outlet></router-outlet>chevron_rightfilter_none -
home.component.html
<h1>GeeksforGeeks</h1>chevron_rightfilter_none -
dash.component.html
<h1>Hey GEEKS! Welcome to Dashboard</h1>chevron_rightfilter_none
Output:
Run the development server and click on the links:

Recommended Posts:
- Routing in Angular JS using Angular UI Router
- Angular 7 | Angular Data Services using Observable
- Angular CLI | Angular Project Setup
- How to enable cURL in PHP?
- How to make “spoiler” text in github wiki pages?
- How to enable PHP's openssl extension to install Composer ?
- jQuery | disable/enable an input element
- How to enable Bootstrap tooltip on disabled button ?
- How to enable/disable all input controls inside a form element using jQuery ?
- How to make sure clients have enough words in textarea by using angularjs in order to disable/enable a button?
- Bootstrap 4 | Navigation Bar
- Bootstrap | Navigation Bar
- How to change navigation bar color in Bootstrap ?
- jQuery | Vertical Dot Navigation Plugin
- HTML Course | Creating Navigation Menu
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


