'void'类型中不存在属性'map'。在角度

时间:2018-03-23 09:34:52

标签: angularjs typescript ionic-framework ionic3

我开发了一个角度5和Ionic 3 app.I使用以下tuitorial。

  
    

https://www.techiediaries.com/ionic-3-http-module-rxjs/

  

但是,我收到了以下错误

  
    

错误是:'void'类型中不存在属性'map'。

  

我的代码(文件:remote-service.ts)

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import {Http ,Response } from '@angular/http';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

/*
  Generated class for the RemoteServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class RemoteService {

getApiUrl : string = "https://jsonplaceholder.typicode.com/posts";

  constructor(public http: HttpClient) {
    console.log('Hello RemoteServiceProvider Provider');
  }

  getPosts() {

    return  this.http.get(this.getApiUrl)
            .do((res : Response ) => console.log(res.json())
            .map((res : Response ) => res.json())  // <-- error shows here
            .catch(error => console.log(error)));
}


}

2 个答案:

答案 0 :(得分:1)

你错过了a)

.do((res : Response ) => console.log(res.json()))

最后,要阅读httpClient的完整响应,必须明确地观察它:

https://angular.io/guide/http#reading-the-full-response

答案 1 :(得分:0)

再次检查是否从有角而不是从Selenium中导入了HttpClient模块。 做这个。

import { HttpClient } from '@angular/common/http';