https://angular.io/docs/ts/latest/guide/router.html
The Angular Router enables navigation from one view to the next as users perform application tasks.
It can interpret a browser URL as an instruction to navigate to a client-generated view and pass optional parameters along to the supporting view component to help it decide what specific content to present
https://vsavkin.com/angular-2-router
The base tag tells the router how to compose navigation URLs.
<head>
<base href="/">
// AppModule.ts
import { RouterModule, Routes } from '@angular/router';
// AppModule.ts
const appRoutes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'heroes', component: HeroesComponent },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes)
],
declarations: [ ... ],
bootstrap: [ ... ]
})
<router-outlet></router-outlet>
<nav>
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
</nav>
//*.component.ts
// import
import { Router } from '@angular/router';
...
// injection
constructor(private router: Router) {}
...
// navigation
this.router.navigate(['/dashboard']);
const appRoutes: Routes = [
{ path: 'hero/:id', component: HeroDetailComponent }
];
<a *ngFor="let hero of heroes" [routerLink]="['/hero', hero.id]">
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
export class HeroDetailsComponent implements OnInit {
hero$: Observable<Hero>;
constructor(
private activatedRoute: ActivatedRoute,
private heroService: HeroService
) { }
ngOnInit() {
this.hero$ = this.activatedRoute.params.pipe(
switchMap(params => this.heroService.getHero(params.id))
);
}
}