10.a Routing basics Router links
AIM: Create multiple components and add routing to provide navigation between them.
Install angular client 13.0 globally, if it is not present in your system
COMMAND:
npm install -g @angular/cli@13.0
create a new angular project abc
ng new abc
go into abc folder and create components
ng generate component home
ng generate component about
ng generate component contact
Open app-routing.module.ts and add routes:
---------------------------
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { ContactComponent } from './contact/contact.component';
const routes: Routes = [
{ path: '', component: HomeComponent }, // default route
{ path: 'about', component: AboutComponent },
{ path: 'contact', component: ContactComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
home.component.html
---------------
<h2>Welcome to Home Page</h2>
<p>This is the home component.</p>

Comments
Post a Comment