I am new to Ionic.
This course on Lynda is my introduction to Ionic and I've been following the instructor tutorial successfully until he introduced providers. My provider file is as follows:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
/*
Generated class for the PeopleDataProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class PeopleDataProvider {
constructor(public http: HttpClient) {
console.log('Hello PeopleDataProvider Provider');
}
getPeople(){
return this.http.get('https://randomuser.me/api/?results=10&nat=us&seed=d07ade5f51ff3011')
.map(res => res.json())
.map(res => res.results);
}
}
My home.ts file is as follows:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { DetailPage } from '../detail/detail';
import { PeopleDataProvider } from '../../providers/people-data/people-data';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [PeopleDataProvider]
})
export class HomePage {
public people = new Array();
private detailPage;
constructor(public navCtrl: NavController, public peopleData:PeopleDataProvider) {
peopleData.getPeople().subscribe(people => {
console.log('people',people);
this.people = people;
});
this.detailPage = DetailPage;
}
loadDetail(person){
console.log(person);
this.navCtrl.push(this.detailPage, {person:person});
}
}
When the app loads I receive the following error:
Typescript Error
Property 'json' does not exist on type 'Object'.
src/providers/people-data/people-data.ts
return this.http.get('https://randomuser.me/api/?results=10&nat=us&seed=d07ade5f51ff3011') .map(res => res.json()) .map(res => res.results);
This post suggested the introduction of import 'rxjs/add/operator/map';
as the solution.
However, that import does not fix the issue for me.
Extra complication:
The error changes to the following exception after a couple of refreshes to the browser but I can't predict when nor control when it occures:
ionic Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PeopleDataProvider -> HttpClient]:
I don't know if the two errors are related.