Last active
August 17, 2017 10:06
-
-
Save dherges/a8f8ce8643010dd65ad3052842eded1e to your computer and use it in GitHub Desktop.
Angular HttpClient (1)
This file contains 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 { NgModule } from '@angular/core'; | |
import { HttpClientModule } from '@angular/common/http'; | |
import { FooBarService } from './foo-bar.service'; | |
@NgModule({ | |
imports: [ HttpClientModule ], | |
providers: [ FooBarService ] | |
}) | |
export class FooBarModule {} |
This file contains 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 { HttpClient } from '@angular/common/http'; | |
// Migration to the new HttpClient is straightforward thanks to dependency injection | |
@Injectable() | |
export class FooBar { | |
constructor( | |
// This used to be `http: Http` - Replace it! | |
private http: HttpClient | |
) {} | |
public foo(): Observable<Object> { | |
// CAUTION | |
// In the "old" `@angular/http`, this returned an `Observable<Response>` by default | |
// With `@angular/common/http`, we now get an `Observable<Object>` on the response body | |
// In effect, we can basically remove all our `.map(res => res.json())` lines :-) | |
this.http.get('/foo/bar').subscribe(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment