fred9
November 15, 2016, 4:10pm
1
Hi everyone!
On app launch, I want to declare a constant APPVERSION, that I can use app wide. With this code, it is not available for other components. How to do this?
Thanks!
Fred
app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import {
AppVersion,
Device,
} from 'ionic-native';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
appVersion: string;
constructor(
platform: Platform,
) {
platform.ready().then(() => {
AppVersion.getVersionNumber().then((s) => {
this.appVersion = s;
console.log('App version: ' + this.appVersion );
})
});
}
}
export const APPVERSION: any = this.appVersion;
mvrc
November 15, 2016, 6:05pm
2
Create a provider and then import it in every pages.
1 Like
fred9
November 15, 2016, 8:49pm
4
mvrc:
Create a provider
Thanks, I have no idea were to get started. How to create a provider with platform.ready() handlers that are needed for Native ?
You can inject Platform into your provider’s constructor and wrap things in a ready
block inside there.
mvrc
November 15, 2016, 9:04pm
6
You can make a provider like this :
Create src/providers/my-data/my-data.ts :
import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { AppVersion } from 'ionic-native';
@Injectable()
export class MyData {
public appVersion: string
constructor(platform: Platform) {
platform.ready().then(() => {
AppVersion.getVersionNumber().then((s) => {
this.appVersion = s;
console.log('App version: ' + this.appVersion );
});
});
}
}
Then in src/app/app.component.ts :
Add import { MyData } from '../providers/my-data';
Add MyData to providers array in @NgModule () : providers: [MyData, ...]
And import your provider in every components and you can access the data.
fred9
November 16, 2016, 1:41am
7
Works like a charm! thanks a lot!
Fred