I’m trying to read from the local JSON file via service
In the app.module: imported HttpModule and then included it in the imports array as well.
The service.ts looks like that:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
@Injectable()
export class DrinksInfoService {
private baseUrl = 'www/drinksData';
constructor(public http: Http) {}
getDrinksInfo() {
return new Promise(resolve => {
this.http.get(`${this.baseUrl}/drinks.json`)
.subscribe(res => resolve(res.json()));
});
}
}
On the app.component.ts file I imported the service, Http, added it to providers like so:
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Storage } from '@ionic/storage';
import { Http } from '@angular/http';
import { DrinksService} from '../shared/shared';
import { DrinksInfoService } from '../shared/shared';
import { SettingsPage } from '../pages/pages';
import { MainPage } from '../pages/pages';
@Component({
templateUrl: 'app.html',
providers: [ DrinksService,
DrinksInfoService,
Http ]
})
export class MyApp {
rootPage: any = SettingsPage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,
public storage: Storage,
private http: Http)
And finally on my page (page for tab to be more specific), I’ve imported the service and then added property drinksInfo: any; and this method to ionViewDidLoad
Do not instantiate Promises needlessly. Do not declare providers in components unless you are absolutely certain of what you are doing. Declare them in your app module.
Hi friend, im having the same problem right know…
How do you resolve the problem?
I have the httpModule in the array of imports
the http in the array of providers
and this function bellow,
To anyone else reading this that was as confused as me - just remove Http from “Providers” in app.module.ts and make sure that HttpModule is in your imports in app.module.ts - keep private http: Http inside of your page constructor.