Developer Snippet Diary

Routing in Angular | Angular Router | Angular 15+

we can create routing in app.module.ts or seprate file

let suppose we have two components  HomeComponent, AboutusComponent

1.Goto app/app.module.ts and define routes + import routes module +import it

import {  RouterModule, Routes } from '@angular/router';
const routesare:Routes=[
  //{path:'',component:HomeComponent},
  {path:'',redirectTo:'home',pathMatch:'full'},
  {path:'home',component:HomeComponent},
  {path:'about',component:AboutusComponent},
]
 imports: [
    RouterModule.forRoot(routesare)
  ],

in app.component.html remove <app-home></app-home>, <app-aboutus></app-aboutus> and add the following code

<router-outlet></router-outlet>

Now visit http://localhost:4200/home

 

Posted by: R GONDAL
Email: rizikmw@gmail.com