Skip to content

Instantly share code, notes, and snippets.

@rajaramtt
Last active February 19, 2019 18:44
Show Gist options
  • Save rajaramtt/82a393dbde1b9b76e2ef1c75974534f1 to your computer and use it in GitHub Desktop.
Save rajaramtt/82a393dbde1b9b76e2ef1c75974534f1 to your computer and use it in GitHub Desktop.
Route Resolvers in Angular 7
import { Injectable } from '@angular/core';
import { HnService } from './hn.service';
import { Resolve } from '@angular/router';
import { ActivatedRouteSnapshot } from '@angular/router';
@Injectable()
export class HnResolver implements Resolve<any> {
constructor(private hnService: HnService) {}
resolve(route: ActivatedRouteSnapshot) {
return this.hnService.getPost(route.paramMap.get('id'));
}
}
getPost(postId: string) {
const endpoint = 'https://hnpwa.com/api/v0/item';
return this.http.get(`${endpoint}/${postId}.json`);
}
{
path: 'post/:id',
component: PostComponent,
resolve: { hnData: HnResolver }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment