类型“ HttpClientModule”上不存在属性“ request”

时间:2019-04-21 04:16:54

标签: angular

了解HttpClient is being replaced by HttpClientModule,所以我从 HttpClient-> HttpClientModule 出发:

import { Injectable } from '@angular/core';
import 
{ 
  HttpClientModule,
  //HttpClient, <-- original
  HttpRequest,
  HttpEventType,
  HttpResponse,
} from "@angular/common/http";

@Injectable()
export class MyService 
{
  constructor(private http: HttpClientModule) { }   // (private http: HttpClient)  <-- original

  public mymethod( 
    this.http.request(req).subscribe(...);
}

问题在最后一行

  

this.http.request(req).subscribe(...);

是的,http.没事。

2 个答案:

答案 0 :(得分:1)

您必须更改以获取或发布请求,并将构造函数更改为

constructor(private http: HttpClient)

this.http.post(req).subscribe(...);

this.http.get(req).subscribe(...);

您的源代码应为:

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

  HttpClient,
  HttpRequest,
  HttpEventType,
  HttpResponse,
} from "@angular/common/http";

@Injectable()
export class MyService 
{
  constructor(private http: HttpClient) { }  

  public mymethod( 
    this.http.post(req).subscribe(...);
}

答案 1 :(得分:1)

导入并注入HttpClient:

从'@ angular / common / http'导入{HttpClient};

构造函数(         私人http:HttpClient,...

this.http.request将可用,例如:

Sample service

还要确保您使用的是支持上述方法的Angular版本。