The RouterOutlet is one of the router directives that became available to the AppComponent because AppModule imports AppRoutingModule which exported RouterModule. It tells the html where to display the routed html.
Copy <router-outlet></router-outlet>
Copy <nav>
<a routerLink="/heroes">Heroes</a>
</nav>
Create a parameterized link
Copy <routerLink="/detail/{{hero.id}}">
Copy import {RouterModule , Routes } from '@angular/router' ;
import { HeroesComponent } from './heroes/heroes.component' ;
Copy const routes : Routes = [
{ path : 'heroes' , component : HeroesComponent } ,
{ path : '' , redirectTo : '/dashboard' , pathMatch : 'full' } ,
{ path : 'detail/:id' , component : HeroDetailComponent } ,
];
Copy @ NgModule ({
imports : [ RouterModule .forRoot (routes) ] ,
exports : [ RouterModule ]
})
export class AppRoutingModule {}
Copy import { ActivatedRoute } from '@angular/router' ;
import { Location } from '@angular/common' ;
Inject ActivatedRoute which hold info about route and location interacts with the browser. The +
turn the string into an integer
Copy constructor (
private route: ActivatedRoute ,
private location: Location
)
ngOnInit () {
const id = + this . route . snapshot . paramMap .get ( 'id' );
}
Copy goBack(): void {
this.location.back();
}