<base href="/">
export const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'contact', component: ContactComponent }
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes)], // PathLocationStrategy
// imports: [RouterModule.forRoot(appRoutes, { useHash: true })],// or use HashLocationStrategy
exports: [RouterModule]
})
<a class="nav-link" [routerLink]="['contact']">Contact us</a>
Choose where you want to display the view <router-outlet></router-outlet>
{ path: '', redirectTo: 'home', pathMatch: 'full' }
{ path: '**', component: ErrorComponent }
loads modules only when required
const routes: Routes = [
{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) }
];
Guards are used to control access to routes
{ path: 'admin', component: AdminComponent, canActivate: [AuthGuard] }
{ path: 'product/:id', component: ProductDetailComponent }
this.route.snapshot.paramMap.get('id');
<a [routerLink]="['/products']" [queryParams]="{category: 'books'}">Books</a>
this.route.snapshot.queryParamMap.get('category');
const routes: Routes = [
{ path: 'products', component: ProductListComponent, children: [
{ path: ':id', component: ProductDetailComponent }
]}
];
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })]
this.router.events.subscribe(event => {
if (event instanceof NavigationStart) {
// Code to execute on navigation start
}
});