【发布时间】:2018-04-17 03:09:05
【问题描述】:
我读过Ionic,检查一下code,这里也是Sample,我总是失败。 :(。 这是我的代码:
export class HomePage {
private secureStorage: SecureStorage;
private data: SecureStorageObject;
private msg: string;
constructor(public navCtrl: NavController, public platform: Platform,
private toast: ToastController) {
if (platform.is('cordova')) {
platform.ready().then(() => {
this.secureStorage = new SecureStorage();
this.secureStorage.create('demoapp').then(
(storage: SecureStorageObject) => {
this.toast.create({message: 'Storage is ready!',position: 'bottom', duration: 3000}).present();
this.data = storage;
this.data.set('teste','chave')
.then(
data => {
this.setMsg("gravou");
this.toast.create({message: "enable ="+this.msg,position: 'bottom', duration: 3000}).present();
this.data.get('teste')
.then(
data => {
this.setMsg(data);
this.toast.create({message: "content="+this.msg,position: 'bottom', duration: 3000}).present();
},
error => {
// do nothing - it just means it doesn't exist
}
);
},
error => {
// do nothing - it just means it doesn't exist
}
);
},
error => console.log(error)
);
});
} else {
this.toast.create({message: "not enable",position: 'bottom', duration: 3000}).present();
}
}
setMsg(msg: string) {
this.msg = msg;
}
getMsg(){
return this.data.get('teste');
}
showData() {
this.toast.create({message: "show ="+this.getMsg(),position: 'bottom', duration: 3000}).present();
}
上面的代码在“.then(...)”中运行 toast 时正确返回 Key。但是,当尝试在其他地方访问时,例如 showData(),不起作用,返回“未定义”。
为什么“this.data = storage”不能在“create('demoapp').then(...)”之外工作?
如何在其他页面访问存储在“demmoapp”中的密钥?
如果有我可以遵循的教程修复,请与我分享。 我现在从 ionic 开始,开始研究 angular 和 ionic,但我还没有为这个问题做好准备。
运行时间:
#ng -v
Unable to find "@angular/cli" in devDependencies.
Please take the following steps to avoid issues:
"npm install --save-dev @angular/cli@latest"
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 1.7.4
Node: 8.11.1
OS: darwin x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
@angular-devkit/build-optimizer: 0.0.35
typescript: 2.6.2
webpack: 3.8.1
#ionic -v
3.20.0
多合一 mac os x sierra。
感谢
【问题讨论】:
-
是有错误,还是没有数据?
-
返回“未定义”。
标签: angular ionic-framework mobile