Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active August 17, 2017 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dherges/a8f8ce8643010dd65ad3052842eded1e to your computer and use it in GitHub Desktop.
Save dherges/a8f8ce8643010dd65ad3052842eded1e to your computer and use it in GitHub Desktop.
Angular HttpClient (1)
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 {}
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