Angular 5 - Http post方法错误

时间:2018-02-14 11:47:41

标签: angular angular5

我已将我的项目从角度4迁移到5.作为迁移的一部分,我已将HttpModule更改为HttpClientModule。 现在我收到以下错误

: Argument of type '(res: Response) => any' is not assignable to parameter of type '(value: ArrayBuffer, index: number) => any'

代码如下

 apiResponse(auth, apiType) {
      return this.http.post(auth.url, auth.data, auth.options)
                    .map((res: Response) => res.json()).subscribe(
        response => {
          console.log(response)
        }
      );
  }

有关如何解决这个问题的想法吗?

2 个答案:

答案 0 :(得分:0)

使用HttpClientModule,您不必手动提取JSON。所以看起来像这样的服务:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { UserModel } from './user.model';

    @Injectable()
    export class UserService {

      constructor(private http: Http) {}

      list(): Observable<UserModel> {
        return this.http.get('/api/users')
          .map(response => response.json())
      }
    }

可以改写为:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { UserModel } from './user.model';

@Injectable()
export class UserService {

  constructor(private http: HttpClient) {}

  list(): Observable<UserModel> {
    return this.http.get('/api/users');
  }
}

答案 1 :(得分:0)

尝试以下

apiResponse(auth, apiType) {
      return this.http.post(auth.url, auth.data, auth.options).subscribe(response => {
          console.log(response)
        }
      );
  }