Professional Documents
Culture Documents
================================
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HeroesComponent } from './heroes/heroes.component';
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
===============
add roterLink
<a routerLink="/heroes">Heroes</a>
====================================
default route with empty path
========================
route parameters
{ path: 'detail/:id', component: HeroDetailComponent }
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: [ './hero-detail.component.css' ]
})
export class HeroDetailComponent implements OnInit {
hero: Hero;
constructor(
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location
) {}
ngOnInit(): void {
this.getHero();
}
getHero(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.heroService.getHero(id)
.subscribe(hero => this.hero = hero);
}
goBack(): void {
this.location.back();
}
}
=====================================================
<base href="/"> in index.html
==========================================
different route paths
=============================================================
order of the path mathes is important
===============================================
routerLinkActive a drectiv used to add css classes to a link basedon it's active
===================================================
Router events
NavigationStart, RouteConfigLoadStart, RouteConfigLoadEnd, NavigationEnd etc
===========================================
=======================================================
Route guards
CanActivate
CanActivateChild
CanDeactivate
Resolve
CanLoad
===================================================
Lazy loading feature modules
CanLoad guard
===============================================
examle
======================================================
routerOutlet names
<router-outlet name="popup"></router-outlet>
{
path: 'compose',
component: ComposeMessageComponent,
outlet: 'popup'
},
===================================================
canactivate guard
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
console.log('AuthGuard#canActivate called');
return true;
}
}
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard],
}
================================================================
lazy loading of modules
{
path: 'admin',
loadChildren: () => import('./admin/admin.module').then(mod => mod.AdminModule),
},