我正在使用 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/