Ionic 3.20.0 api HttpClient not working


#1

Hi,

My provider api not working if i’m gonna use HttpClient. Please see below api provide code

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class ApiProvider {

  data : any;
  
  apiUrl = 'https://jsonplaceholder.typicode.com';

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

  getUsers() {
    return new Promise(resolve => {
      this.http.get(this.apiUrl+'/users').subscribe(data => {
        resolve(data);
      }, err => {
        console.log(err);
      });
    });
  }


}

my Home.ts includes

import { ApiProvider } from '../../providers/api/api';

  constructor(public navCtrl: NavController, private LoadingCtrl: LoadingController, public api: ApiProvider) {
   this.getUsers();

  }


  getUsers() {
    this.api.getUsers()
    .then(data => {
      this.users = data;
      console.log(this.users);
    });
  }

#2

Have you imported it(HttpClient) in app.module.ts?


#3

ApiProvider is probably not declared in the app module. Also, stop instantiating needless Promises.


#4

Thanks, I forgot to declare httpmodule on app.module.ts