0

Understand HttpClient is being replaced by HttpClientModule, so I'm going from HttpClient --> HttpClientModule as:

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(...);
}

problem is on the last line,

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

yeah, http. has nothing come up.

Jeb50
  • 3,784
  • 4
  • 26
  • 44

2 Answers2

1

You must change to get or post request and change in constructor to

constructor(private http: HttpClient)

And

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

or

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

Your source code should be:

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(...);
}
Hien Nguyen
  • 21,001
  • 7
  • 35
  • 48
1

Import and inject HttpClient:

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

constructor( private http: HttpClient,...

this.http.request will be available, e.g.:

Sample service

Also make sure you are using an Angular version that supports the described approach.

topalkata
  • 1,665
  • 2
  • 10
  • 10
  • HttpClient is being replaced not introduced. – Jeb50 Apr 21 '19 at 04:29
  • 1
    Http was replaced with HttpClient in Angular 4.3 (or so), the correct imports are HttpClientModule in the app.module.ts, and HttpClient in the data service. – topalkata Apr 21 '19 at 04:36
  • HttpClientModule was imported in app.module.ts, HttpClient was imported in MyService now giving me trouble, installed angular version is 7.x – Jeb50 Apr 21 '19 at 23:33
  • maybe the problem of my end. – Jeb50 Apr 22 '19 at 04:19