import { Headers, Http } from '@angular/http';
@Injectable()
export class PublisherService{
private publishersUrl = 'app/publisher';
constructor(private http: Http) { }
getPublishers(): Promise<Publisher[]>{
return this.http.get(this.publishersUrl)
.toPromise()
.then(response => response.json().data)
.catch(this.handleError);
}
}
我得到这个错误:
属性'toPromise'在类型'Observable'上不存在。any
您需要像这样添加操作符:
import 'rxjs/add/operator/toPromise';
你想要使用的每个rxjs操作符都需要这个。
尝试在import语句中添加'Response'像这样:
import {Http, Headers, Response} from '@angular/http';
我还注意到你没有从angular core中导入Ingectable尽管你使用了@Injectable装饰器。
import { Injectable } from '@angular/core';
在开头使用此导入
import {Observable} from "rxjs/Rx";