import { Injectable } from '@angular/core' ; import { ActivatedRouteSnapshot , Resolve , Router , RouterStateSnapshot } from '@angular/router' ; import { Observable , of } from 'rxjs' ; import { map , catchError } from 'rxjs/operators' ; import { Session } from './session.model' ; import { SessionService } from './session.service' ; @ Injectable ( { providedIn : 'root' } ) export class SessionResolver implements Resolve < Session > { constructor ( private sessionService , private router ) { } resolve ( route , state ) { const id = + route . paramMap . get ( 'id' ) ; return this . sessionService . getSession ( id ) . pipe ( map ( session => { if ( session ) { return session ; } return new Session ( ) ; } ) , catchError ( ( error ) => { console . log ( `${ error }. Heading back to session list` ) ; this . router . navigate ( [ '/sessions' ] ) ; return of ( null ) ; } ) ) ; } }