Thanks @rapropos , I am trying your code right now, although I don’t understand fully.
So here’s my global-vars.ts with your code:
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class GlobalVars {
ready: Promise<any>;
private _isWebIntegration: boolean;
constructor(private _storage: Storage) {
this.ready = Promise.all([
this._storage.get('isWebIntegration').then((wip) => this._isWebIntegration = wip),
// &c &c for other similar stuff
]);
}
// don't call any of these until this.ready resolves
isWebIntegration(): boolean {
return this._isWebIntegration;
}
setWebIntegration(wip: boolean): Promise<any> {
this._isWebIntegration = wip;
return this._storage.set('isWebIntegration', wip);
}
}
but when I add the dependency injection in weightlevel.ts, I get an error that was not there before. I added the import
line, the @Component({...providers: [GlobalVars]
the constructor(... public globalVars: GlobalVars
and as follows:
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { ComparePage } from '../compare/compare';
import { ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';
import { LocalStorageService } from 'angular-2-local-storage';
import { GlobalVars } from '../../providers/global-vars';
@Component({
selector: 'page-weightlevel'
, templateUrl: 'weightlevel.html'
, providers: [GlobalVars]
})
export class WeightlevelPage {
name: string;
firstNavParam: boolean;
data:any = {};
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private storageService: LocalStorageService
, public globalVars: GlobalVars
) {
console.log("Executing Weightlevel.ts");
this.name = 'Max';
// this.globalVars.addGlobalVar("isWebIntegration", false);
if (storageService.get('firstNavParam') === 'no') {
this.firstNavParam = false;
} else {
this.firstNavParam = true;
}
this.data.shareoptions = ['Facebook', 'Twitter', 'Email'];
this.data.techniqueText = "Intermediate+";
this.data.frequScaleWording = ['Once a year or +','Once a month or +', 'Once a week or +', 'Once a day or +'];
this.data.fitnessWording = ['Couch potatoe', 'Healthy', 'Sporty', 'Top athlete'];
this.data.levelName = ['Total noob',
'Noob',
'Beginner',
'Intermediate-',
'Intermediate',
'Intermediate+',
'Good',
'Good+',
'Very good',
'Excellent',
'Pro surfer!'];
this.data.slideIndex = 0;
this.data.slideTitle = ['Gender',
'Date of birth',
'Weight',
'Height',
'Technique',
'Frequency',
'Fitness',
'Wetsuit'];
this.data.slideTitleTranslationIDs = [];
for (var i = 0; i < this.data.slideTitle.length; i++) {
this.data.slideTitleTranslationIDs[i] = 'general.' + this.data.slideTitle[i].toLowerCase().replace(/\s/g, '');
}
}
@ViewChild(Slides) slides: Slides;
ionViewDidLoad() {
console.log('ionViewDidLoad WeightlevelPage');
}
goToSlide() {
this.slides.slideTo(2, 500);
}
}
and I now get this error:
Runtime Error
Error in ./MyApp class MyApp - caused by: No provider for Storage!
Stack
Error: DI Error
Another question:
Do you think this or this (putting global variables in a ts file) is a good solution? It looks simpler for sure.