Closed jbeckton closed 7 years ago
its a temporary drop in replacement for @angular/http which will store your api data retrieved by the server so that the client does not have to rerequest them
oh thats slick! Does it make sense to use that api for other things in my code as well or is it just needed for the transferring of state?
@FrozenPandaz
Is there a guide on how to use it?
@crebuh basically on the server side you do your query and save the results in transfer-state and on the client side you query the transfers-state
in a provider something like:
getSomething(): Observable<Object> {
if (isPlatformServer(this.platformId)) {
return Observable.fromPromise(this.doGet());
} else {
return this.getFromTransferState();
}
private doGet(itemHashId: string): Promise<{}> {
return new Promise((resolve, reject) => {
const params: HttpParams = new HttpParams();
this.httpClient.get('http://...', {params: params}).subscribe((result: any) => {
this.transferState.set('my-key', result);
resolve(result);
}, (error) => {
reject(error);
});
});
}
private getFromTransferState(): Observable<Object> {
return new Observable(observer => {
observer.next(this.transferState.get('my-key'));
});
}
don't know if it's the right way or a good way but that seems to work for me
angular universal will two http call, one in the server when you realize the request to generate the html render to SEO, and other call in the client when the code js is load in the browser. the purpose of transfer is to avoid the second call and transfert the info of the http call to the client. or wherever info you need to transfer to the client not only http calls
sorry for my bad enghlish.
@ysus that's good english 👍
If I may, I would just suggest to modify angular universal need to
with angular universal will
... it isn't a must, that's the purpose of transfer-state ;)
@peterpeterparker
thx I got this, maybe my question wasn't clear. the module offers already some methods like get, put, post.
so this was confusing me, if these are just examples inside the class. but it seems you are just working with with transferState.get and transferState.set
@crebuh yes so far, simple case at least, I'm only working with TransferState
What is the purpose of the TransferHttpModule