Module09 - Routing Flashcards
Module 9 - Routing
Import
import {Routes, RouterModule} from ‘@angular/router’;
//in .ts file
import { AddressService } from ‘./address.service’;
Module 9 - Routing
Configure
//In .ts file
const approutes: Routes = [
{ path: “”, component: DavisComponent },
{ path: “davis”, component: DavisComponent },
{ path: “trafalgar”, component: TrafalgarComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(approutes)
],
providers: [AddressService]
})
Module 9 - Routing
Use
//In HTML file
<button (click)=”campusAddress(‘davis’)”>Get Address</button>
What are the steps required to set up routing in an Angular application?
The steps include:
Importing Routes and RouterModule in app.module.ts.
Defining routes as an array of path-component pairs.
Registering routes using RouterModule.forRoot(approutes).
How do you define a route in Angular, and what does the RouterModule.forRoot() method do?
A route is defined using the Routes array where each route includes a path and a component. The RouterModule.forRoot() method registers these routes with the Angular application.
Example:
const approutes: Routes = [
{ path: “”, component: DavisComponent },
{ path: “davis”, component: DavisComponent },
{ path: “trafalgar”, component: TrafalgarComponent }
];
@NgModule({
imports: [RouterModule.forRoot(approutes)],
})
How do you link to a specific route using the routerLink directive in an Angular template?
Use the routerLink directive in an anchor tag or button element to navigate to a specific route.
Example:<a routerLink="/davis">Davis Campus</a>