Angular 2 读取 native 存储的最佳位置

标签 angular

我正在使用 iconic 2/angular 2 和 NativeStorage 插件。

我有一个允许用户更改的服务器 URL,它应该被保留。我是 Angular 的新手,所以不确定调用 NativeStorage.getItem 的最佳位置。

显然它应该是用户启动应用程序时最先出现的地方,并且 NativeStorage 已经初始化

我当前将应用程序卡在加载屏幕上的代码:

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)
            );

      });
    });
  }
}

最佳答案

我们从 app.module.ts 和配置开始

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

我的应用程序.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();
        });
     }
}

关于Angular 2 读取 native 存储的最佳位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340131/

相关文章:

angular - 类型错误 : Cannot Read Property 'CreateObjectURL' Of Undefined

angular - Firebase实时数据库中有 "Multi-Path Push"这样的东西吗?

node.js - Express 无法从后面读取未定义的属性 'name'

c# - Angular 6 - 单个表单组中的多个文件输入

typescript - Angular 2 RC2 单选按钮绑定(bind)损坏

javascript - 是否可以使用由不同版本的 Angular 构建的不同 Angular 元素

angular - Electron Angular 库错误

unit-testing - "Can' t resolve all parameters for MdDialogRef : (? )”测试NG2 Material Dialog组件时出错

json - 将用户数据添加到本地存储只工作一次,此后被添加的对象被简单地覆盖? ( Angular 10+)

Angular 和 NGX-Masonry - 显示问题