Last active
February 19, 2019 18:44
-
-
Save rajaramtt/82a393dbde1b9b76e2ef1c75974534f1 to your computer and use it in GitHub Desktop.
Route Resolvers in Angular 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getPost(postId: string) { | |
const endpoint = 'https://hnpwa.com/api/v0/item'; | |
return this.http.get(`${endpoint}/${postId}.json`); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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