Skip to content

Instantly share code, notes, and snippets.

@evertonrobertoauler
Created October 10, 2017 13:00
Show Gist options
  • Save evertonrobertoauler/6a376da8e25f3eccb37d581f1e530827 to your computer and use it in GitHub Desktop.
Save evertonrobertoauler/6a376da8e25f3eccb37d581f1e530827 to your computer and use it in GitHub Desktop.
universal-demo-v5/src/app/app.component.ts 3
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