I am trying to inject a service to get information from the jsonplaceholder RESTful API into a component. I keep getting the error below:
EXCEPTION: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: this._postService.getPosts(...).susbcribe is not a function
Here is my code:
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule, JsonpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { PostService } from './post.service';
@NgModule({
imports: [ BrowserModule, HttpModule, JsonpModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [ PostService ]
})
export class AppModule { }
app.component.ts:
import { Component } from '@angular/core';
import { PostService } from './post.service';
@Component({
selector: 'my-app',
template: ``
})
export class AppComponent {
constructor(private _postService: PostService){
this._postService.getPosts()
.susbcribe(posts => console.log(posts));
}
}
post.service.ts:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable();
export class PostService {
private _url = "http://jsonplaceholder.typicode.com/posts";
constructor(private _http: Http){
}
getPosts(){
return this._http.get(this._url)
.map(res => res.json());
}
createPost(post){
return this._http.post(this._url, JSON.stringify(post))
.map(res => res.json());
}
}
Thank you for your help!
You got a typo:
susbcribe
instead of
subscribe