Created
October 10, 2017 13:00
-
-
Save evertonrobertoauler/6a376da8e25f3eccb37d581f1e530827 to your computer and use it in GitHub Desktop.
universal-demo-v5/src/app/app.component.ts 3
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 { Component, OnInit } from '@angular/core'; | |
import { HttpClient } from '@angular/common/http'; | |
import { TransferState, makeStateKey } from '@angular/platform-browser'; | |
const DOGS_KEY = makeStateKey('dogs'); | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.scss'] | |
}) | |
export class AppComponent implements OnInit { | |
title = 'app'; | |
dogs: any; | |
constructor( | |
private http: HttpClient, | |
private state: TransferState | |
) { } | |
ngOnInit() { | |
this.dogs = this.state.get(DOGS_KEY, null as any); | |
if (!this.dogs) { | |
this.http | |
.get('https://dog.ceo/api/breeds/list/all') | |
.subscribe(data => { | |
this.dogs = data; | |
this.state.set(DOGS_KEY, data as any); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment