我正在尝试注入一个服务,以便将jsonplaceholder RESTful API中的信息转换为组件。我一直收到以下错误:
EXCEPTION: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: this._postService.getPosts(...).susbcribe is not a function
这是我的代码:
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());
}
}
感谢您的帮助!
答案 0 :(得分:1)
你有一个错字:
susbcribe
而不是
subscribe