Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 best place to read native storage

Tags:

angular

I am using iconic 2/angular 2 and using NativeStorage plugin.

I have a server URL that I Let user to change and it should be persisted. I am very new to angular and so was not sure what should be the best place to make a call to NativeStorage.getItem.

Obviously it should be a place which is very first when user launch the app everything and by that NativeStorage is already initialized

my current code that hangs the app on loading screen:

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 { NativeStorage } from 'ionic-native';

import { HomePage } from '../pages/home/home';
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, storage: NativeStorage) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
       platform.ready().then(() => {

          NativeStorage.getItem('CHAT_SERVER_HOST').then(
            (val) => { alert("init:" + val); },
            error => alert(error)
            );

      });
    });
  }
}
like image 470
Vik Avatar asked Dec 08 '25 08:12

Vik


1 Answers

We start from app.module.ts and config

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
  ],
  providers: []
})
export class AppModule {}  

myapp.component.ts

export class MyApp {  

  constructor(platform: Platform, storage: Storage) {
     storage.get('your_item').then((val) => {
         console.log(val);
         this.platformReady();
     });

     platformReady() {
        // Call any initial plugins when ready
        this.platform.ready().then(() => {
           this.splashScreen.hide();
        });
     }
}
like image 162
Thien Hoang Avatar answered Dec 10 '25 06:12

Thien Hoang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!