Learning ionic 2, particularly using Storage
.
学习离子2,特别是使用存储。
So, I just created a blank app:
所以,我刚创建了一个空白的应用程序:
ionic start storagetest blank --v2
Following this the docs:
在此之后的文档:
cordova plugin add cordova-sqlite-storage --save
npm install --save @ionic/storage
Then, my app.module.ts
looks like this:
然后,我的app.module.ts看起来像这样:
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { IonicStorageModule } from '@ionic/storage';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
IonicModule.forRoot(MyApp),
IonicStorageModule.forRoot()
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
And then went ahead to home.ts
:
然后继续回家。
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, storage: Storage) {
storage.ready().then(() => {
storage.set('name', 'Max');
storage.get('name').then((val) => {
console.log('Your name is', val);
})
});
}
}
There is nothing in the javascript log. It appears that get()
is never returning the value. However, ready()
does work, as I have put a console.log()
in it.
javascript日志中没有任何内容。似乎get()永远不会返回值。但是,ready()确实有效,因为我在其中放了一个console.log()。
What is wrong then?
那有什么不对?
I am running the app on Chrome, Mac OS.
我正在Chrome,Mac OS上运行该应用。
1 个解决方案
#1
2
storage.set
is asynchronous and returns a promise.So value may not be set when get().then()
is called. Try:
storage.set是异步的,并且在调用get()。then()时可能不会设置promise.So值。尝试:
storage.ready().then(() => {
storage.set('name', 'Max').then(()=>
storage.get('name').then((val) => {
console.log('Your name is', val);
});
);
});
You have no error handler in then
or catch()
method which is probably why nothing is logged.
你没有错误处理程序或catch()方法,这可能是为什么没有记录任何东西。
#1
2
storage.set
is asynchronous and returns a promise.So value may not be set when get().then()
is called. Try:
storage.set是异步的,并且在调用get()。then()时可能不会设置promise.So值。尝试:
storage.ready().then(() => {
storage.set('name', 'Max').then(()=>
storage.get('name').then((val) => {
console.log('Your name is', val);
});
);
});
You have no error handler in then
or catch()
method which is probably why nothing is logged.
你没有错误处理程序或catch()方法,这可能是为什么没有记录任何东西。