首页 文章

属性'toPromise'在类型'Observable<Response>'上不存在

提问于
浏览
70
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'类型上不存在

4 回答

  • 0

    你需要像这样添加运算符:

    import 'rxjs/add/operator/toPromise';
    

    这是您想要使用的每个rxjs运算符所必需的 .

  • 156

    尝试从'@ angular / http'向您的import语句添加'Response',如下所示:

    import {Http, Headers, Response} from '@angular/http';
    

    另外我注意到你没有从角度核心导入Ingectable,尽管你使用@Injectable装饰器 .

    import { Injectable } from '@angular/core';
    
  • 7

    一开始就使用这个导入

    import {Observable} from "rxjs/Rx";

  • 10

    对于其他任何偶然发现此事的人(它是我的顶级谷歌链接),请参阅下面的链接答案之一

    https://github.com/Microsoft/TypeScript/issues/8518#issuecomment-229506507

    正如它所说,在Visual Studio 2015中,您可以通过更新您的打字稿版本来解决此问题

    https://www.microsoft.com/en-us/download/details.aspx?id=48593

相关问题