Make constant from app.component available app wide

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;

Create a provider and then import it in every pages.

1 Like

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.

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.

Works like a charm! thanks a lot!
Fred